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:
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.