0 x00 the

Those of you who do Linux development might run into this dilemma:

  1. After two hours of running a program, you think it will run in an hour, and you are excited to see the results.
  2. His girlfriend just freaked out and showed up right away.
  3. You suddenly realize that you are not using Nohup, which means that the program will die after you leave. The previous two hours of running time were wasted.
  4. Regretfully, you press CTR+ C and run the program again using Nohup.

Wait, this nohup can be remedied. Here’s how to do it.

0x01 Fault Description

1.1 Why Do I Stop a Process

When the user logout or the network is disconnected, the terminal receives an HUP signal and shuts down all of its child processes.

The reason is: when an SSH session is closed, the PTY associated with SSH is closed, and the system sends SIGHUP signals to all processes in the session associated with the PTY. The default SIGHUP signal handler is to terminate the process unless the process itself has processed SIGHUP.

So our solution is in two ways:

  • Either let the process ignore the HUP signal;
  • Or the process can run in a new session and become a child process that does not belong to the terminal;

1.2 nohup role

The nohup command does three things to the process.

  • stopSIGHUPSignals are sent to this process.
  • Turn off standard input. The process can no longer accept any input, even when running in the foreground.
  • Redirects standard output and standard error to filesnohup.out.

That is, the nohup command effectively separates the child process from its session. So when the shell window is closed, the nohup process is not terminated.

0 x02 briefly

What can I do if I forget to use NohUP? Specific operations are as follows:

2.1 Operation Sequence

The specific operation sequence is as follows:

  1. For a running process, you can use “CTRL+ Z” to suspend the current process in the background.
  2. At this time, the process has entered the background pause, we use “jobs” to find the suspended process, each background task has a jobnumber (task serial number, non-PID).
  3. Use “bg jobNumber “to make the process run in the background;
  4. Use “jobs” again to check the process status. The process is in the running state.
  5. Use the disown command “disown -h %jobnumber” so that the process performs the same function as nohup. Now you’re done.
  6. If you want to continue, you can run the ps command to check the process status.
  7. You can use “fg” to turn the background task into the foreground task. At this time, you can perform operations on the process, such as ending;

2.2 the sample

We take running a Redis as an example to give the specific operation sequence, as shown in the following figure:

mylinux $ redis-server
* The server is now ready to accept connections on port 6379
^Z
[1]+  Stopped                 redis-server
mylinux $ jobs
[1]+  Stopped                 redis-server
mylinux $ bg 1
[1]+ redis-server &
mylinux $ jobs
[1]+  Running                 redis-server &
mylinux $ disown -h %1
mylinux $ ps -elf| grep redis
501 1987 521 4006 0 31 0 4289624 1932 - T 0 ttys001  0:00.01 redis-server *:6  9:49Mylinux $fg redis-server ^C1987:signal-handler (1616291836) Received SIGINT scheduling shutdown...
1987:M 21 Mar 09:57:16.634 # User requested shutdown...
1987:M 21 Mar 09:57:16.634 * Saving the final RDB snapshot before exiting.
1987:M 21 Mar 09:57:16.641 * DB saved on disk
1987:M 21 Mar 09:57:16.641 # Redis is now ready to exit, bye bye...      
Copy the code

0 x03 principle

Let’s analyze the principles of operation commands one by one.

3.1 CTRL + Z

Ctrl+Z suspends the current program and pauses execution of the program.

mylinux $ redis-server
* The server is now ready to accept connections on port 6379
^Z
[1]+  Stopped                 redis-server  
Copy the code

The program is suspended and goes into the background. Can suspend many processes to the background.

3.2 jobs

The jobs command displays the number of jobs currently running in the background.

In Linux, the ability to start, stop, terminate, and resume a job is collectively known as job control. The key command in job control is the Jobs command, which allows you to see what jobs the shell is currently processing. The jobs command outputs plus and minus signs. Jobs with plus signs are regarded as default jobs, and jobs with minus signs are the next default jobs.

Once the current default work is processed, the work with a minus sign automatically becomes the new default work. In other words, no matter how many jobs are running at this point, there will be only one plus and one minus sign job at any given time.

We can see that the redis-server is running in the background, [1] indicates that the process number is 1.

mylinux $ redis-server
* The server is now ready to accept connections on port 6379
^Z
[1]+  Stopped                 redis-server
mylinux $ jobs
[1]+  Stopped                 redis-server  
Copy the code

3.3 bg

The bg command can change a command that is paused in the background to be continued in the background.

mylinux $ redis-server
* The server is now ready to accept connections on port 6379
^Z
[1]+  Stopped                 redis-server
mylinux $ jobs
[1]+  Stopped                 redis-server
mylinux $ bg 1
[1]+ redis-server &  
Copy the code

After using bg, you can see that the output redis-server is followed by an &, indicating that it is running in the background.

We can also use Jobs again to check the process status.

mylinux $ redis-server
* The server is now ready to accept connections on port 6379
^Z
[1]+  Stopped                 redis-server
mylinux $ jobs
[1]+  Stopped                 redis-server
mylinux $ bg 1
[1]+ redis-server &
mylinux $ jobs
[1]+  Running                 redis-server &  
Copy the code

3.4 disown

The disown command removes a specified task from the “background tasks” list (the result of the Jobs command). As long as a “background task” is not in the list, the session will definitely not send it a SIGHUP signal. This has the same effect as nohup.

mylinux $ redis-server
* The server is now ready to accept connections on port 6379
^Z
[1]+  Stopped                 redis-server
mylinux $ jobs
[1]+  Stopped                 redis-server
mylinux $ bg 1
[1]+ redis-server &
mylinux $ jobs
[1]+  Running                 redis-server &
mylinux $ disown -h %1   
Copy the code

3.5 ps

When disown is used, the target job is removed from the job list and we can no longer view it using Jobs, but we can still find it using ps-ef.

mylinux $ redis-server
* The server is now ready to accept connections on port 6379
^Z
[1]+  Stopped                 redis-server
mylinux $ jobs
[1]+  Stopped                 redis-server
mylinux $ bg 1
[1]+ redis-server &
mylinux $ jobs
[1]+  Running                 redis-server &
mylinux $ disown -h %1
mylinux $ ps -elf| grep redis
501 1987 521 4006 0 31 0 4289624 1932 - T 0 ttys001  0:00.01 redis-server *:6  9:49In the morningCopy the code

3.6 fg

The fg command can move the commands running in the background to the foreground. If the number of tasks running in the background is large, you can select jobNumber (task serial number, not PID).

In addition, if there are two task ids running in the background, after task 1 is executed, task 2 will be the current task. In this case, when you run fg and bg commands without job number, the current task will be changed by default.

mylinux $ redis-server
* The server is now ready to accept connections on port 6379
^Z
[1]+  Stopped                 redis-server
mylinux $ jobs
[1]+  Stopped                 redis-server
mylinux $ bg 1
[1]+ redis-server &
mylinux $ jobs
[1]+  Running                 redis-server &
mylinux $ disown -h %1
mylinux $ ps -elf| grep redis
501 1987 521 4006 0 31 0 4289624 1932 - T 0 ttys001  0:00.01 redis-server *:6  9:49Am myLinux $fg Redis-serverCopy the code

0 XFF reference

www.ibm.com/developerwo…

www.ibm.com/developerwo…

Nohup command _ master these several commands, Linux background task submission, front background task conversion play casually

The Jobs command for Linux

The Linux jobs command displays the jobs placed in the background on the current terminal

How to start the Linux daemon process

In Linux, nohup keeps programs running in the background after the shell is turned off

Nohup: Close the shell program and still execute