RussNS
RussNS

Reputation: 863

Execute background process after script has exited

Question up front (TLDR style) since this is a long-ish post: Is there a better way to kick off 2 chained commands (i.e. cmd1;cmd2) as background processes in a script while allowing the original script to exit fully?

A similar question to this was asked before 3 years ago, but it didn't cover my needs. This question was ultimately answered by having the background process kick off as the script was exiting. I have a specific need to have this background process kick off after the script has exited.

I have a situation where I need a script to run fully and exit, but I need the script to execute 2 background tasks, in order after the script exits.

The tasks are these:

sleep 5
service proprietaryAgent restart

The sleep is there only to give the originating script time to exit. So I guess this command could take place as the script is exiting. The rub is, the "proprietaryAgent". This agent is a mechanism that's allowing me to run a script remotely. So restarting it during the script is problematic. I need to run a script that will start a background job then exit, and that background job needs to be essentially "wait a bit (to give the original script time to fully exit), then restart this agent."

I've tried:

sleep 5;service proprietaryAgent restart &
disown
exit 0

And I've tried:

nohup sleep 5;nohup service proprietaryAgent restart &
exit 0

I'm pretty sure I see the logic fail there, in that starting sleep as a foreground process, then the script owns the 'sleep 5' envocation and will not exit until sleep has completed, which restarts the proprietaryAgent as the script is exiting. However, the requirement of this question is the proprietaryAgent must start AFTER the script exits, not during the exit or before.

I also tried:

trap "nohup sleep 5;nohup service proprietaryAgent restart &" 0
exit 0

But trap detects the exit, and kicks off then runs fully, THEN allows the script to fully exit. So that didn't work.

So like a goober I tried:

nohup sleep 5 &;nohup service proprietaryAgent restart &
exit 0

I don't honestly know what this does. I tried it just to try it. But in this context (I think) sleep is itself a background process, and disconnected from the chaining ";" that would cause the next command to wait on it.

Lastly I tried this (basically have a script write a 2nd script, and kick that off 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, though it still needs troubleshooting, but it's back to the original question. Is there a "better" way that I should be approaching this?

Upvotes: 3

Views: 1002

Answers (1)

Gene
Gene

Reputation: 341

The last thing you tried should work fine. You could bypass writing the command to a separate file and do something like this instead:

nohup bash -c "sleep 10 ; service opsware-sas restart opswgw" &

Another option is to use the at command to schedule a job in the future. For example:

at -f /path/to/script.sh now + 1 minute

You would need an external script that calls the service opsware-sas restart opswgw command for you pre-made (or a temporary one created by the parent script).

Upvotes: 3

Related Questions