Keep Scripts Running Forever

Whether you're automating processes, managing server chores, or just experimenting; you often need a way to keep your scripts running after disconnect or indefinitely. I see people using mostly nohup
& screen
for this, but there are some other tools available as well.
Let's discuss the less well-known but convenient ways to run scripts or commands on a Linux machine forever—even after logging out—in this blog post.
But first, let's us do a refresher of known methods:
1️⃣ nohup
nohup while true; do dig codereliant.io NS; sleep 1; done &
This command uses nohup
to ignore the HUP (hangup) signal, which is sent to the process when the user logs out. The output of the command will be redirected to a file named nohup.out
by default, unless you specify otherwise.
The command while true; do dig codereliant.io NS; sleep 1; done
is a loop that continuously runs the dig
command to retrieve the NS records for the domain codereliant.io
, with a 1-second delay between each query. And we will use this as an example throughout the post.
Alternatively, using screen
or tmux
can be very effective. These tools allow you to start a session, run the command, and then detach from the session while leaving it running on the server.
Start a new
screen
ortmux
session:
screen
Run your command
while true; do dig codereliant.io NS; sleep 1; done
Detach from the screen session by pressing
Ctrl-A
followed byD
.
This way, the process will keep running in the background inside the screen session, even if you log out, and you can reattach to it later if needed.
For tmux, you use tmux
to start a new session, and Ctrl-B
followed by D
to detach from the screen.
3️⃣ disown
Sometimes in a machine or mostly in a running docker container you might not have nohup
, screen
, or tmux
installed, you can use another approach to keep your command running in the background even after you log out or detach from the container by using the shell's job control features along with redirection of outputs to avoid the terminal sending a HUP
(hangup) signal.
The disown
command in bash removes a shell job from the shell's job table, making it immune to the HUP
signal when you log out. Here’s how you can use it:
Start the process in the background as usual:
while true; do dig codereliant.io NS; sleep 1; done &
Then you can use
jobs -l
to see the running jobs and their job numbers.Use
disown
with the job number to remove the job from the shell’s job table:
disown %1
Replace
%1
with the actual job number if it's different.
4️⃣ setsid
Another tool you might have available is setsid
, which runs a program in a new session. This means the program will not be tied to the terminal session and will continue running after logout:
setsid sh -c 'while true; do dig codereliant.io NS; sleep 1; done' &
This command wraps your loop in a sh -c
command string, ensuring it can handle the loop syntax correctly, and starts it in a new session using setsid
.
Redirecting Outputs
In all these cases, it’s a good idea to redirect stdout and stderr to a file or /dev/null
to prevent the process from trying to write to a non-existent terminal, which could cause it to stop:
while true; do dig codereliant.io NS; sleep 1; done >/dev/null 2>&1 &
Adding >/dev/null 2>&1
redirects both the standard output and the standard error to /dev/null
, effectively discarding all output.
Using any of these methods, you should be able to run your command in the background effectively without having traditional utilities like nohup
or screen
.
If you liked this post and want to learn more about similar concepts, subscribe to our newsletter! We'll keep you updated with more tips, tricks, and in-depth guides to ensure you're always a step ahead in managing your technology.
Hey there, reader! 👋
You know what they say, sharing is caring! If you found this newsletter helpful please take a moment to share it with your friends, colleagues and social media followers. Thanks for being a part of this community! 🚀