Running process in background after closing terminal
Asked Answered
D

3

19

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.

Doolie answered 27/8, 2015 at 7:48 Comment(7)
Tangentially relevant: mywiki.wooledge.org/BashFAQ/050Clemen
Having a new process start each time you launch a new shell sounds like a terrible idea.Clemen
@Clemen I know of the security issues with this. But I'd rather try to get this working first before I address that. At this point I'm not sure if running a process in the background with a closed terminal is even possible.Doolie
The warning message seems to come from the OSX Terminal app; it is certainly not a feature of Bash. Have you checked whether the process remains running in spite of the warning? If not, do you know what kills it?Clemen
The process (cmd I defined) runs after the terminal starts up. When I attempt to close the terminal, the process is still running. Only after I close the terminal window is cmd stopped. I will try to examine the OSX terminal preferences to see if its responsible.Doolie
Okay I got rid of the warning by toggling the OSX terminal preferences, but the problem still exists (cmd still gets killed).Doolie
You mean you changed the "Ask before closing" preference? That just disables the dialog, obviously.Clemen
C
28

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.

Clemen answered 27/8, 2015 at 8:12 Comment(0)
B
14

This is already answered, but the Screen utility seems like it would be perfect for this.

  • man screen To view the documentation for screen.

  • www.ss64.com/osx/screen.html to view slightly more user friendly documentation.

  • Start screen with a name and a script to run:

    screen -S GWatch Scripts/gw_watch.sh
    This starts a screen session named 'GWatch' and executes gw_watch.sh.
    

When a screen session is started, one has the option of disconnecting from it. This will leave the screen active in the background. It will remain active even after the user logs out (permissions notwithstanding).

Here is an example:

  1. Create a shell script called 'screencheck.sh'
  2. Put the following into the file (I often use textwrangler and / or nano).

    #!/bin/bash
    
    count=0
    
    while [ $count -lt $1 ] ; do 
       echo "Count: $count of $1. Pausing for five seconds."
       sleep 5s
       ((count++))
    done
    
  3. Open two terminal windows.

  4. In one of the terminal windows type screen -ls. You should see a message about no sockets being found.
  5. In the second terminal window, change directory to where the script was saved.
  6. In the second terminal window type screen -S ScreenCheck screencheck.sh 500. screencheck.sh has to be executable.
  7. In the second terminal window, you should see:

    Count: 0 of 500. Pausing for five seconds.
    Count: 1 of 500. Pausing for five seconds.
    Count: 2 of 500. Pausing for five seconds.
    ...
    
  8. Disconnect from the screen session by typing ctrl-a d. That's control + a, release both, d key.
  9. You should see [detached].
  10. In the first terminal, type screen -ls.
  11. You should see something like:

    FCH000: ~: screen -ls
    There is a screen on:
       1593.ScreenCheck (Detached)
    1 Socket in /var/folders/pk/l6b5fhkj6mxfpfh8mtgmstg40000gn/T/.screen.
    
  12. Reattach to the screen session using screen -R ScreenCheck.

  13. You should see something like:

    Count: 226 of 500. Pausing for five seconds.
    Count: 227 of 500. Pausing for five seconds.
    Count: 228 of 500. Pausing for five seconds.
    Count: 229 of 500. Pausing for five seconds.
    ...
    

To see if it is running after logout, log out and ssh to the computer from another computer. screen -ls should show the same screen session as before.

I hope this helps.

Bushed answered 8/11, 2016 at 15:23 Comment(1)
Screen is very old. It's what I use, too, but many people have switched to tmux.Clemen
I
0

nohup is not a cross platform solution.

Linux(others):

nohup [terminal-command] > [output-file-name] &

Windows:

START /B [command-prompt-command] 2>&1

There is a better solution that works on all platform i.e. pm2. We can run any script using pm2.

npx pm2 start "my-test -script' --name test"
npx pm2 list
npx pm2 delete test
Inferno answered 20/7, 2022 at 7:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.