I have two scripts say 'S1' and 'S2'. I execute these scripts as,
nohup S1 &
nohup S2 &
But I would like them to execute sequentially. ie., S2 should execute only on successful completion of S1. How should I go about doing this?. How can I know when S1 finishes execution?. Any examples would be much appreciated. Thanks.
You can use the & to start multiple background jobs. This will start multiple jobs running in the background. If you want to keep a job running in the background, once you exit the terminal you can use nohup .
You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time. This will display the process for the current shell with four columns: PID returns the unique process ID.
You can use the ps command to list all background process in Linux. Other Linux commands to obtain what processes are running in the background on Linux. top command – Display your Linux server's resource usage and see the processes that are eating up most system resources such as memory, CPU, disk and more.
Running shell command or script in background using nohup command. Another way you can run a command in the background is using the nohup command. The nohup command, short for no hang up, is a command that keeps a process running even after exiting the shell.
You can execute them, sequentially, like this:
(nohup S1 && nohup S2) &
Try
(echo 1 && sleep 1 && echo 2) &
The double ampersand operator is described here.
Note that when using &&
, S2
only runs if S1
finishes "successfully" (return code 0). This seems to be what you wanted. If you want S2
to run regardless of whether S1
succeeds, use ;
instead of &&
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With