Reputation: 10093
I have a task/process currently running. I would like to schedule another task to start when the first one finished.
How can I do that in linux ?
(I can't stop the first one, and create a script to start one task after the other)
Upvotes: 0
Views: 445
Reputation: 151
If you want to execute your second task every time the first one is not running, the answer suggested by tink works:
watch -n 1 'pgrep <name of task1> || <task2>'
However, I wanted to run task2
only once as soon as task1
was finished. So I used:
watch -n 1 -g 'pgrep <name of task1>'; <task2>
Upvotes: 0
Reputation: 101181
You want wait.
Either the system call in section 2 of the manual, one of it's varients like waitpid
or the shell builtin which is designed explicitly for this purpose.
The shell builtin is a little more natural because both processes are childred of the sell, so you write a script like:
#!/bin/sh
command1 arguments &
wait
command2 args
To use the system calls you will have to write a program that forks, launches the first command in the child then wait
s before execing the second program.
The manpage for wait (2)
says:
wait() and waitpid()
The wait() system call suspends execution of the current process until one of its children terminates. The call wait(&status) is equivalent to:waitpid(-1, &status, 0);
The waitpid() system call suspends execution of the current process until a child specified by pid argument has changed state.
Upvotes: 0
Reputation: 15213
Somewhat meager spec, but something along the line of
watch -n 1 'pgrep task1 || task2'
might do the job.
Upvotes: 2