Saturday, April 9, 2022

[SOLVED] Running process in background after closing terminal

Issue

I am trying to run a script in the background even after closing the terminal. I have searched and searched and tried nohup and disown but neither seem to be working. When I close a terminal window, I get the typical Closing this window will terminate the running processes: watch. message. That ends up terminating my background process, even when using nohup or disown. What could be the problem?

My code is a simple two lines

cmd="nohup watch -n 1 sudo /etc/block.sh > /dev/null"
$cmd & # blocks automatically  

It is located in .bash_profile, because I want it to start up whenever I open a new terminal.

You can ignore the sudo; I've already found a way to execute a sudo command without entering the password.

I am using Mac OSX.


Solution

Starting a subshell and running the nohup command from there seems to avoid having Terminal kill it off when exiting.

bash -c "nohup sh -c 'while true; do date; sleep 1; done' &"

Not very elegant, but works for me.



Answered By - tripleee
Answer Checked By - Candace Johnson (WPSolving Volunteer)