DEV Community

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

Posted on

1 1 1 1 1

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)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay