skip to Main Content

I need to run a Perl script for several days processing something. On a linux Centos server, from the SSH terminal I run this command:

nohup perl script.cgi 2>&1 &

This runs the script in the background and writes the output to nohup.out.
The problem when I close the SSH terminal or even my internet connection disconnects the script terminates.

I need to keep this command running in the background on the server after I close
the SSH terminal.

3

Answers


  1. Chosen as BEST ANSWER

    The only way I was able to run the command and exit the shell and keep the command running is using the "at" tool to schedule the job like this using the full script path:

    echo "perl /home/username/www/script.cgi" | at now + 1 minute
    

  2. you can either use screen or run the command using supervisor in linux systems.

    • you can install screen using sudo apt install screen
    • then use following command to run it.
    • screen -S test_command
    • nohup perl script.cgi 2>&1 &
    • Then press ctrl+a and ctrl+d to leave that session running for whatever amount of time required until your server reboots.
    • If you want to stop the command use screen -x test_command, then ctrl+c and use ctrl+a and ctrl+d to close screen or ctrl+a and ctrl+d to leave the screen session as it is.
    Login or Signup to reply.
  3. You can use Terminal multiplexer tools like screen, byobu or tmux.
    I personally use screen. so install it on remote server via sudo apt-get install screen.

    1. ssh into server
    2. open screen session by screen -S sessionname
    3. Now run your command (background/foreground both works)
    4. now detach to your session by command ctrl+a then press d.
    5. Now shut your pc and enjoy
    6. now come back ssh into server then use command screen -x sessionname to reconnect the detached session.

    Hurray! script is still running.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search