Execute background process after script exits
Question up (TL; DR style), since this is a long post: Is there a better way to run the two attached commands (i.e. cmd1; cmd2) as background processes in a script while still allowing the original script to exit entirely?
A similar question was asked 3 years ago, but it didn't suit my needs. This question was eventually answered when the background process started from the script exiting. I have a specific need to start this background process after the script exits.
I have a situation where I need a script to run and exit completely, but I need the script to execute 2 background jobs so that after the script exits.
The tasks are as follows:
sleep 5
service proprietaryAgent restart
Sleep only to give the script's initial time to exit. So my guess is that this command can be executed as the script exits. Rubbing is "property". This agent is a mechanism to allow a script to be run remotely. So restarting it during a script is problematic. I need to run a script that will start a background job and then close, and that background job has to essentially "wait a little" (so that the original script time out completely), then restart that agent. "
I tried:
sleep 5;service proprietaryAgent restart &
disown
exit 0
And I tried:
nohup sleep 5;nohup service proprietaryAgent restart &
exit 0
I'm sure I can see that the logic is not working there, in that starting sleep as foreground process, then the script owns the "sleep 5" envocation and won't exit until the sleep has finished, which restarts the native asgent as the script exits. However, the requirement of this question is that the owner must start AFTER the script exits, and not during the exit or earlier.
I've also tried:
trap "nohup sleep 5;nohup service proprietaryAgent restart &" 0
exit 0
But the trap detects the exit and runs entirely, THEN allows the script to exit completely. So it didn't work.
Since goober I tried:
nohup sleep 5 &;nohup service proprietaryAgent restart &
exit 0
I honestly don't know what it does. I tried just try it. But in this context (I think) the sleep itself is a background process and is disconnected from the ";" which will make the next command wait on it.
Finally I tried this (basically the script wrote the 2nd script and hit this in the background):
echo "#!/bin/bash" > /var/tmp/restartAgent.sh
echo "sleep 5" >> /var/tmp/restartAgent.sh
echo "service proprietaryAgent restart" >> /var/tmp/restartAgent.sh
chmod 755 /var/tmp/restartAgent.sh
nohup /var/tmp/restartAgent.sh &
exit 0
I think I'm close here, although it still needs troubleshooting, but it goes back to the original question. Is there a "better" way I should be approaching this?
source to share
The last one you tried should work fine. You can bypass writing the command to a separate file and do something like this:
nohup bash -c "sleep 10 ; service opsware-sas restart opswgw" &
Another option is to use the command at
to schedule the job in the future. For example:
at -f /path/to/script.sh now + 1 minute
You will need an external script that invokes the command service opsware-sas restart opswgw
for the previously created (or temporary, created by the parent script).
source to share