The nohup command is a useful tool that enables us to run a process in the background while protecting it from the SIGHUP termination signal. For example, with nohup, we can run a shell script from the terminal, and it will be running even after this terminal is closed. However, sometimes we need extra knowledge to run more complex Bash commands using nohup.
In this tutorial, we’ll learn how to execute the Bash for loop with nohup.
2. for Loop Command
Firstly, let’s quickly refresh on for loops:
$ for i in /home/*; do echo "$i"; done /home/user1 /home/user2
Here, we list the files and directories in the /home directory via globbing and iterating through the result.
Let’s look at the command in more detail:
- for i in /home/*; do starts iterating over all files and directories in /home
- echo “$i” prints each file name
- done closes the for loop and, in this case, finishes the program
Now that we know what our command is doing, let’s try to run it using nohup.
3. Run for Loop With nohup
Let’s try to directly execute a for loop with nohup:
$ nohup for i in /home/*; do echo "$i"; done > output.log & bash: syntax error near unexpected token `do'
Since nohup uses system calls and doesn’t go through the shell with its arguments, passing shell code directly to it presents a challenge and may result in an error. Since the for loop is a Bash script syntax, the program returns a syntax error if the for loop snippet is supplied unmodified.
So, we need other means to avoid this error.
3.1. Using the bash -c Syntax
To run the above command with nohup, we can use a specific syntax:
$ nohup bash -c 'COMMAND' > output.log &
In this example, we have several steps:
- bash -c ‘COMMAND’ runs COMMAND as a Bash shell script (we can also use the regular sh)
- > output.log saves the command output to a log file
- & puts everything in the background
Let’s run nohup, using our for loop in the place of COMMAND:
$ nohup bash -c 'for i in /home/*; do echo "$i"; done' > output.log &  9445 nohup: ignoring input and redirecting stderr to stdout
Notably, a PID (process identification number) has been assigned to the background nohup process. In our case, the PID is 9445. This means our for loop is now running out of sight.
The next output line tells us that nohup is ignoring input and redirecting stderr to stdout. This is a standard nohup message and practice. Consequently, our nohup for loop won’t accept any input and in case of errors, we’ll see them on stdout.
3.2. Using a Script
Of course, we can also use the code above in a script with similar results:
$ cat script.sh for i in /home/*; do echo "$i" done $ chmod +x script.sh $ nohup ./script.sh > output.log &
After making the script executable via chmod, we simply use it instead of the bash -c command, thus avoiding the hassle of one-liners.
4. Check nohup Output
Now that we started the nohup command, we can check the log file to see if it runs correctly. So, we need to read the output.log file.
Let’s use the cat command for our purposes:
$ cat output.log /home/user1 /home/user2
As we can see, we have the directories in /home listed correctly. So, our for loop has done its job using the nohup command. Of course, for long-running processes, monitoring via tail -f can be useful.
In this article, we learned how to run the nohup command with a for loop.
Firstly, we compiled a simple for loop command. Secondly, we studied the nohup syntax to run a complex bash command as well as a simple script. And finally, we saw how to check our log file to ensure the program runs correctly.