Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequentially executing background processes unix

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.

like image 961
wowrt Avatar asked Jun 19 '10 10:06

wowrt


People also ask

How run multiple processes in background Linux?

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 .

How can we list out all currently executing background processes?

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.

What command in Linux brings up all the processes running in the background?

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.

How do you run a process in the background in Unix shell script?

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.


1 Answers

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 &&.

like image 159
Stephen Avatar answered Sep 28 '22 01:09

Stephen