You can use the background control operator (&) to run a process in the background and the sleep
command to wait before running a second process, i.e.:
#!/usr/bin/env bash
# script.sh
command1 &
sleep x
command2
Here is an example of two commands that print out some time-stamped messages:
#!/usr/bin/env bash
# Execute a process in the background
echo "$(date) - Running first process in the background..."
for i in {1..1000}; do
echo "$(date) - I am running in the background";
sleep 1;
done &> background-process-output.txt &
# Wait for 5 seconds
echo "$(date) - Sleeping..."
sleep 5
# Execute a second process in the foreground
echo "$(date) - Running second process in the foreground..."
for i in {1..1000}; do
echo "$(date) - I am running in the foreground";
sleep 1;
done
Run it to verify that it exhibits the desired behavior:
user@host:~$ bash script.sh
Fri Dec 1 13:41:10 CST 2017 - Running first process in the background...
Fri Dec 1 13:41:10 CST 2017 - Sleeping...
Fri Dec 1 13:41:15 CST 2017 - Running second process in the foreground...
Fri Dec 1 13:41:15 CST 2017 - I am running in the foreground
Fri Dec 1 13:41:16 CST 2017 - I am running in the foreground
Fri Dec 1 13:41:17 CST 2017 - I am running in the foreground
Fri Dec 1 13:41:18 CST 2017 - I am running in the foreground
Fri Dec 1 13:41:19 CST 2017 - I am running in the foreground
Fri Dec 1 13:41:20 CST 2017 - I am running in the foreground
...
...
...
sleep
halts process-A? Can you show the test process you're using, or output indicative of this? If process-A is halting, it's more likely that it's trying to read from the terminal while running in the background and getting halted for that reason, rather than anything related tosleep
.process_a </dev/null &
will attach its stdin to/dev/null
rather than the TTY, and that may be sufficient to avoid the problem.