Hello All, I am new to Unix operating system and I have a doubt about what is the ‘nohup’ in UNIX. Yesterday I try to understand the architecture of Unix and I found the term ‘nohup’. Can anyone tell me some examples?
Hello,
why not GOOGLE or
Regards
Hello,
Welcome to the forum ! We hope you find this to be a friendly and helpful place, and that you enjoy your time here.
The command nohup is used to run a command in such a way that it will persist even if or when its parent shell receives a HUP signal. Signals are methods of communication with and between processes, and the hang-up (or HUP) signal) is sent to a process to let it know that its terminal has disconnected, and that it should therefore now do whatever it needs to do to handle that situation (which normally means that it will clean up and exit).
The command nohup is provided to enable a command to continue to run in the background even after you log out. nohup will run your script or binary in such a way as to ensure that it will not be affected by a HUP signal when you log out, and so it never exits as a result.
Now, on most modern Linux systems, the Bash shell by default doesn't actually send a HUP signal to your background processes when you log out anymore, so this is a bit less necessary than it used to be. But if you plan on running a long-lived script or other binary in the background and think or suspect that you'll want or need to disconnect and leave it running, then running it via nohup is a good idea.
Let's demonstrate with a little example. Imagine we have a script that we want to run in the background, like so:
$ ./script.sh &
[1] 31060
$
Here, we see our script is now running, with a PID of 31060. We can verify that it is running like so:
$ ps 31060
PID TTY STAT TIME COMMAND
31060 pts/1 S 0:00 /bin/bash ./script.sh
$
Now, if we tell Bash that we want to enable the old-school behaviour of sending a HUP to our background processes when we log out, and then log out and back in, we will see this script is no longer running:
$ shopt -s huponexit
$ exit
logout
<we log back in>
Last login: Tue Aug 23 12:43:11 BST 2022 on pts/1
$ ps 31060
PID TTY STAT TIME COMMAND
$
And as you can see, the script is no longer there.
Now, if we run the same script using nohup under the same circumstances, it will persist, like so:
$ shopt -s huponexit
$ nohup ./script.sh &
[1] 31826
$ nohup: ignoring input and appending output to 'nohup.out'
$ ps 31826
PID TTY STAT TIME COMMAND
31826 pts/1 S 0:00 /bin/bash ./script.sh
$ exit
logout
<we log back in>
Last login: Tue Aug 23 12:59:26 BST 2022 on pts/1
$ ps 31826
PID TTY STAT TIME COMMAND
31826 pts/1 S 0:00 /bin/bash ./script.sh
$
And as you can see, our script is still running via nohup, despite all our background processes having been sent a HUP signal when we logged out.
Hope this helps !
I checked on google and found one source, they mentioned nohup is a special command to run a process in the background even when a user logs off from the system. It also helps in creating daemon processes or some cleanup script of logs. Is it correct?
Yes, correct. Both of those are examples of where the processes need to do their designed task after their associated processes have ceased to run.
A daemon is a freestanding process that provides a service to processes that have not even started yet. It gets started by another process, and hovers in the system until something connects to it; or it may have been given a schedule to carry out (like a crontab which the cron daemon refers to every minute).
Cleaning up logs is a similar process: once a log file is closed by a process, a cleanup process may be left running to compress any previous versions and archive older ones, to avoid continuous growth (often called housekeeping).
Hi Athira,
Welcome and thanks for the question.
Using nohup is not as popular as it was in days past.
Many system admins use utilities like the followingto accomplish routine system admin tasks, making nohup less popular today than it once was:
Personally, I use screen more than tmux, but I have no idea why off the top of my head (because have not used tmux for a while). Often when I start remote tasks, I will ssh into the server and immediate run screen to insure that if my session dies (or the process hangs) for any reason, I can login again and not lose any process time related to the running task.
You might find it instructive, @athira, to review the documentation above in your quest to understand nohup and other high level process tasks.
Additional Information
After posting, I visited the nohup wiki page posted originally, and I noticed that screen is referenced as follows on the wiki:
Alternatives
...
For example, the following invocation of screen will run somescript.sh in the background of a detached session:
$ screen -A -m -d -S somename ./somescript.sh &
As a final note, I do not run screen as in the example above, and being a lazy **** I generally just run:
ubuntu$ screen
... and go from there, but you have inspired me to dig a bit deeper next opportunity.
