Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write pid to file

I know this could be easy, but I am unable to figure out. We want to run two instances of luigi on one machine so need to modify init.d script to write PID to file rather than just touching empty file.

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

At present its just touching the empty PID file. I want it to write PID into the file. Also while stopping I want to kill by PID stored in PID file

echo -n $"Stopping luigid scheduler: "
killproc luigid
RETVAL=$?
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/luigid && echo_success || echo_failure
echo

Any help please ?

Thanks

like image 798
roy Avatar asked Dec 09 '25 04:12

roy


1 Answers

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_failure
echo
return $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.

like image 164
arco444 Avatar answered Dec 10 '25 19:12

arco444



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!