3

I have a script that is running on a terminal and it prints some output. I want to send it to the background without pressing a key (and without script modification) after 5 seconds, for example. Something like a bash script that start my script, sleeps for some time and send the running app to background. I don't care about output once it has gotten into the background.

UPD: What DOESN'T works:

  • nohup
  • Usage of & after script - it send it to background immediately while output still going to terminal (that is what I want to avoid)
  • Have you tried anything? you can simply use "&" to send a job in the background, "sleep 5" to wait for 5 sec and put it together in a shell script. – Pacifist Sep 10 at 6:28
  • 3
    yes, use sleep(5) && bg to push it to the background – dustblue Sep 10 at 6:39
  • @Pacifist_AWS it doesn't work: npm 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 at 11:01
  • @dustblue thank but how can I use it? For instance I running npm start - once it started I cannot enter anything to console - no prompt. If I put sleep/bg before start it obviously waits for 5 seconds and sends nothing to background. – Alex G.P. Sep 10 at 11:03
1

There are no non-hacky ways to redirect the output of another process elsewhere [1], but if a) you don't plan to ever bring your process back to foreground and b) are OK with another process (a light-weight cat) also running for the whole time your program runs, this may do:

{ your_program ... & } | (trap : INT; cat & sleep 5; kill $!; cat >/dev/null &)

You may want to redirect both the stderr and stdout of your program into the pipe with your_program 2>&1.

[1] the possible hacky way being to attach with a debugger to it and redirect its output from inside it.

  • It looks like black magic but seem it exactly what I am looking for. Thank you. – Alex G.P. Sep 10 at 18:28
  • feel free to ask if anything is unclear; the trap : INT's purpose is to make a ^C kill just the sleep 5, not the whole (...) subshell, which would cause the output of your_program to continue to go through the 1st cat, instead of switching to the 2nd cat >/dev/null. (your_program & and cat & will not be killed by a ^C). – mosvy Sep 11 at 23:13
0

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

-2

You can create a .desktop configuration file, and link it with the bash file. By this, you can execute the shell file, and it won't open up in the terminal. Just be sure to change the Terminal=True to Terminal=False To not open in the terminal. Type the below in a file called "anyfile.dekstop" and make it executable (right click - properties - allow executing file as program)

[Desktop Entry]

Version=1.0

Exec="path to your executable file or .sh file"

Name=Sample

GenericName=Sample

Comment=ANy comment

Encoding=UTF-8

Terminal=false

Type=Application

Categories=Application

Icon=any icon link

  • I am asking for terminal solution, bash script. – Alex G.P. Sep 10 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.