How do I write pid to file How do I write pid to file shell shell

How do I write pid to file


You can get the PID of the previous command with $!

Change the start script to:

echo -n "Starting luigid scheduler as $LUIGID_USER: "/usr/bin/sudo -u $LUIGID_USER $LUIGID_BIN >>/var/log/luigid/server.log 2>&1 &RETVAL=$?PID=$![ $RETVAL -eq 0 ] && echo $PID > /var/lock/subsys/luigid && echo_success || echo_failureechoreturn $RETVAL

Note you need to remove the brackets from around the command as they start the program in a subshell. The subshell will not return the PID of the started program back to your shell, so you should call it directly from your shell.


(man) shlock should do the trick for that I do believe :

shlock [-du] [-p PID] -f lockfile

...

shlock uses the link(2) system call to make the final target lock file, whichis an atomic operation (i.e. "dot locking", so named for this mechanisms original use for locking system mailboxes).  It puts the process ID ("PID") from the command line into the requested lock file.

Or depending on the situation (man) flock, for a great example check this out :http://codehackit.blogspot.fr/2013/04/locking-processes-with-flock.html