Now I see a big misunderstanding: I thought the job should start in foreground and change to the background magically after 5 seconds, and output doesn't matter. With this answer though, output continues...Q seems to equal "background" with "no output". Well this is interesting anyway:
With this helper script 'suspender'...
#!/bin/bash
sleep 1
pid=$(pidof -x counter.sh)
sleep 3
kill -s STOP $pid
sleep 1
kill -s CONT $pid
...I can suspend a script called 'counter.sh' and make it restart in the background. The call is:
./suspender & ./counter.sh
"Suspender" starts in the background and waits a aecond until counter is running. It gets the pid and waits 3 seconds (the delay as requested). The STOP signal corresponds to ctrl-Z
, CONT to bg
. The sleep between is necessary.
Counter is a program you want to watch a few seconds and often ctrl-c it. After five seconds it should free the prompt. Output doesn't matter. This just prints 10 numbers, getting slower.
#!/bin/bash
for i in {1..10}
do
sleep $i; echo $i
done
With the job control messages and the output continuing this does not feel too nice.
Before the suspend-resume-in-bg action you can stop the script with ctrl-c, and afterwards you can use the prompt. It almost makes sense...
sleep(5) && bg
to push it to the background – dustblue Sep 10 '19 at 06:39npm start&
sends to background but next usage of sleep doesn't make sense - it is ALREADY in background. My question is about start app and after 5 seconds send it to background that is different. – Alex G.P. Sep 10 '19 at 11:01npm start
- once it started I cannot enter anything to console - no prompt. If I putsleep/bg
before start it obviously waits for 5 seconds and sends nothing to background. – Alex G.P. Sep 10 '19 at 11:03