Introduce two methods to avoid interruption of tasks in execution

Sometimes we may have a need to execute a command or script, but it takes hours or even days. This has to consider a problem, that is, if the network is disconnected or other unexpected situations occur, what should I do if the task is interrupted? You can throw commands or scripts into the background, but they are not safe. Here are two ways to avoid such problems.

Use nohup

[root@localhost ~]# cat /usr/local/sbin/sleep.sh

#! /bin/bash

Sleep 1000

[Root @ localhost ~] # nohup sh /usr/local/sbin/sleep.sh &

[1] 19997

[root@localhost ~]# nohup: Ignore the input and append the output to "nohup.out"

Adding a '&' directly to the background, but when exiting the terminal, it is very likely that the script will also exit, and adding nohup in front of it is no problem, the role of nohup is to run the command without hanging.

2. Use of the screen tool

Simply put, screen is a window manager that can multiplex a physical terminal between multiple processes. There is a concept of session in screen, users can create multiple screen windows in a screen session, just like operating a real SSH connection window in each screen window. Below I Ming introduces a simple application of screen.

1) Open a session, enter the screen command directly and press Enter to enter the screen session window. If you do not screen command, use yum install -y screen installation.

[root@localhost ~]# screen

[root@localhost ~]#

2) screen -ls View the screen session that has been opened

[root@localhost ~]# screen -ls

There is a screen on:

20001.pts-0.localhost (Attached)

1 Socket in /var/run/screen/S-root.

3) Ctrl +a Press d again to exit the screen session, just exit and it doesn't end. At the end, type Ctrl +d or enter exit

4) want to log in again after exiting a screen session, use sreen -r [screen number], this is the number 20001 on the case. When there is only one screen session, the following numbers can be omitted. When you have a command or script that needs to run for a long time, open a screen session and run the task. Press ctrl +a and press d to exit the session without affecting any operations on the terminal window.

Introduce two methods to avoid interruption of tasks in execution

Security Aluminum Alloy Die Casting

Security Aluminum Alloy Die Casting,Aluminium Alloys Die Casting,Aluminum Alloy Die Casting Mold,Aluminum Housing Aluminum Alloy Dies

Dongguan Metalwork Technology Co., LTD. , https://www.dgdiecastpro.com