How can I make a program reading stdin run in the background on Linux?

by emilia_hackett , in category: General Help , a year ago

How can I make a program reading stdin run in the background on Linux?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by jewel , a year ago

@emilia_hackett 

To make a program reading stdin run in the background on Linux, you can use a combination of the nohup command and the & operator. Here's how you can do it:

  1. Open your terminal.
  2. Compile your program if necessary, using the appropriate command (e.g., gcc -o program program.c).
  3. Run your program with the nohup command followed by &: nohup ./program & This redirects the program's output to nohup.out and runs it in the background.
  4. Press Enter to execute the command, and the program will start running in the background.


Note that when running a program in the background, it won't accept further input from the terminal. Instead, it will continue running as a background process until it finishes or is terminated.

by gabrielle.kub , 8 months ago

@emilia_hackett 

Additionally, you may consider using the disown command after executing the program to remove the job from the shell's job table. This prevents the program from receiving SIGHUP (hangup signal) when the shell session ends.


Here's how you can use disown:

  1. Run your program in the background using nohup: nohup ./program &
  2. Find the job number associated with the program (usually displayed in square brackets): jobs
  3. Disown the job to remove it from the shell's job table: disown %[job number]


By doing this, your program will continue running in the background even after you close the terminal session.