Posts tagged shell

Stop child-processes from keeping lock (LockHandler)
0This kept me busy for a while. I have a queueing system that run constantly on our servers – built in PHP because, why now 🙂
Basically I have the controller, run as: php app/console aqueue:controller --bootstrap  – this merely starts, and one by one run the child-processes and disowns them, like so:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php foreach ($this->queue_repo->findAll() as $Queue) { /** @var Queue $Queue */ $cmd = 'php app/console aqueue:parent --queue-name=' . $Queue->getName() . ' --env=' . $input->getOption('env') . ' > /dev/null 2>&1 < /dev/null'; echo $cmd . PHP_EOL; $p = new Process($cmd); $p->start(); // TODO: Figure out why when we don't dump it afterward, it doesn't remain running after this Command quites: } |
However, the problem is – in order to make sure somebody doesn’t run the bootstrap script more than one at the same time, I get a lock at the top of the console command before continuing – and if I can’t get the lock, it exits immediately. It’s a safety measure.
I get the lock like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
protected function initialize(InputInterface $input, OutputInterface $output) { parent::initialize($input, $output); // Lock (so we don't run multiple of these at the same time): $this->lockFile = $this->getName() . '.lock'; $this->lockHandler = new LockHandler($this->lockFile); if (!$this->lockHandler->lock()) { echo 'Sorry, can\'t get the lock. Bailing out!' . PHP_EOL; exit; } } |
The problem is, once you start the child-process – each of them ALSO holds the lock that the aqueue:controller created above. Thus, when the bootstrapping controller is done, but the child processes are still running (as they should be) – it can’t start the bootstrapper again because the lockfile is held by other processes.
To fix this: Simple, release the lock.
So to do this easily and elegantly, you merely create a destructor that releases the lock in the bootstrapping command, like so:
1 2 3 4 5 6 7 8 9 10 |
public function __destruct() { /** * This is important to do, because the controller --boostrap intention is to run the aqueue:parents commands and dies, * and then run again to make sure they are still there through crontab */ if (isset($this->lockHandler)) { $this->lockHandler->release(); } } |
Once this is done, the lockfile inheritance problem goes away 🙂

Make a file or folder invisible in Mac OS X Finder
0I wanted to make the files that my new WD Live creates for it’s library invisible in my mac. It creates two files for each movie, a movie.metathumb file and a movie.xml, where “movie” is the name of the movie, say “Alice In Wonderland.avi”.
When you install XCode (Apple Mac Developer tools) you get a little script called “SetFile”, which lives in:Â /usr/bin/SetFile
You can run the following commands to make these files invisible to your Finder:
1 2 |
SetFile -a V /path/to/yourMovieFolder/*.metathumb SetFile -a V /path/to/yourMovieFolder/*.xml |
PS: To make them visible again, you’ll notice that it’s a capital “V”, just run the same with lower case “v”:
1 2 |
SetFile -a V /path/to/yourMovieFolder/*.metathumb SetFile -a V /path/to/yourMovieFolder/*.xml |
It worked wonderfully!
Now all I have to do is create a shell script that gets run each time I mount my drive when I’m copying movies on to it 🙂