DEV Community

Cover image for Leaving a long script running in the background from a SSH session
Talles L
Talles L

Posted on

Leaving a long script running in the background from a SSH session

nohup ./my-script.sh >> my-output.txt 2>&1 &
Enter fullscreen mode Exit fullscreen mode

nohup stands for "no hangup", which prevents the SIGHUP (signal hangup) that is sent when closing the SSH session (or closing a local terminal) to terminate the script.

>> it's redirecting the output and appending to a custom file, if you omit you will get the output on a file called nohup.out.

2>&1 makes not only stdout to be redirected but stderr as well.

& puts the command in the background and gives you the CLI back to be used.

Here's a dummy script for you to test it out:

#!/bin/bash

while true
do
    echo "5 seconds just passed"
    sleep 5
done
Enter fullscreen mode Exit fullscreen mode

Remember to kill it after you are done!

Top comments (0)