Several Ways of Running linux in the Background Summary of of

  • 2021-07-22 11:59:25
  • OfStack

1. nohup

Run the program in a way that ignores the suspend signal

Supplementary notes
The nohup command can run the program by ignoring the suspend signal, and the output information of the run program will not be displayed to the terminal.
Whether the output of the nohup command is redirected to the terminal or not, the output is appended to the nohup. out file in the current directory.
If the nohup. out file in the current directory is not writable, the output is redirected to the $HOME/nohup. out file.
The command specified by the command parameter cannot be invoked if no file can be created or opened for appending.
If the standard error is 1 terminal, all output written to the standard error by the specified command is redirected to the same file descriptor as the standard output.

Simple example:


nohup command &

Specify the output instance


nohup command > myout.file 2>&1 &

Other related commands
ctrl + z # can put a command that is being executed in the foreground in the background and is in a paused state.
fg # Switches background tasks to foreground execution
bg # changed a command that was paused in the background to continue in the background. If there are multiple commands in the background, you can call the selected command out with bg% jobnumber
jobs # View background running status, jobs-l options display PID for all tasks
ps-ef grep command or ps aux grep command # View Process
kill-9 process id # kills the corresponding process,

More advanced usage is as follows:

ps aux grep command grep-v grep awk '{print $1}' xargs kill-9 # This representation gets the process id directly through command and drops it directly through kill

2. screen

Used for command line terminal switching

a, Session Recovery
As long as Screen itself is not terminated, sessions running inside it can be resumed. This one point is especially useful for users who log in remotely-even if the network connection is interrupted,
Users will not lose control of command-line sessions that have been opened. Simply log on to the host again and execute screen-r to resume the session.
Also, when you leave temporarily, you can also execute the separation command detach, and let Screen hang (switch to the background) while ensuring the normal operation of the program inside

b, multi-window
In the Screen environment, all sessions run independently and have their own numbers, inputs, outputs, and window caches. Users can switch under different windows through shortcut keys,
And can freely redirect the input and output of each window. Screen implements basic text operations, such as copy and paste; It also provides functions like scroll bars,
You can view the history of window status. Windows can also be partitioned and named, and the activity of background windows can be monitored. Session sharing Screen allows one or more users
Log in to one session multiple times from different terminals and share all the features of the session (for example, you can see exactly the same output). It also provides a mechanism for window access rights,
Windows can be password protected.

Installation of c, screen


yum install -y screen

Basic grammar
screen -AmRvx -[ls -wipe][-d < Job name > ][-h < Number of rows > ][-r < Job name > ][-s ][-S < Job name > ]

Options
-A adjusts all windows to the current terminal size.
-d < Job name > Offlines the specified screen job.
-h < Number of rows > Specifies the number of buffer rows for the window.
-m Force the creation of a new screen job even if an screen job is already in the job.
-r < Job name > Resume an offline screen job.
-R attempts to resume an offline job first. If no offline job is found, a new screen job is created.
-s Specifies the shell to execute when a new window is created.
-S < Job name > Specifies the name of the screen job.
-v displays version information.
-x Resume screen jobs that were previously offline.
-ls or--list displays all current screen jobs.
-wipe checks all screen jobs currently available and deletes screen jobs that are no longer available.

Common screen parameters
screen -S yourname - > Create a new session called yourname
screen -ls - > List all current session
screen -r yourname - > Back to yourname, this session
screen -d yourname - > Remote detach Some session
screen -d -r yourname - > End the current session and return to the yourname session

Under each screen session, all commands start with ctrl + a (Ctrl + a).

Ctrl + a ? - > Display all key binding information
Ctrl + a c - > Create a new window to run shell and switch to it
Ctrl + a n - > Next, switch to the next window
Ctrl + a p - > Previous, switch to the first window
Ctrl + a 0..9 - > Switch to 0.9 th window
Ctrl+a [Space] - > Switch from Window 0 to Window 9 in sequence
Ctrl + a Ctrl + a - > Switch between two recently used window
Ctrl + a x - > To lock the current window, you need to unlock it with the user password
Ctrl + a d - > detach, temporarily leave the current session, throw the current screen session (which may contain multiple windows) to the background for execution, and return to the state before entering screen. At this time, in screen session, process running in each window (whether foreground/background) continues to execute, even if logout does not affect.
Ctrl + a z - > Put the current session in the background for execution, and use the fg command of shell to go back.
Ctrl + a w - > Show a list of all windows
Ctrl + a t - > time, showing the current time, and load of the system
Ctrl + a k - > kill window, forcibly shutting down the current window
Ctrl + a - > Enter copy mode, and under copy mode, you can roll back, search and copy just like using [vi 1]
C-b Backward, PageUp
C-f Forward, PageDown
H (capital) High, move the cursor to the upper left corner
L Low, move the cursor to the lower left corner
0 to the beginning of the line
End of $line
w forward one word, moving forward in words
b backward one word, moved backward in words
Space the first press is the starting point of the labeled area, and the second press is the end point
Esc End copy mode
Ctrl + a ] - > paste, paste the content just selected in copy mode

For an example of operation, see: https://wangchujiang.com/linux-command/c/screen.html

3. daemonize tool


## Installation 
git clone git://github.com/bmc/daemonize.git
sh configure && make && sudo make install

-a # mounts to the output file instead of flushing out the default values. Only when-e and/or-o is specified is applied.
-e # Redirects output standard error to specified file instead of/dev/null
-o # Redirects the output standard to the specified file instead of/dev/null
-E name = value # Adds environment variables to the daemon. This parameter type 1 must be in the format name=value. Parameter can be set multiple times.
-c directory # Customize the directory before running the command.
-p pidfile # Customize your own pid storage location.
-l lockfile # This file will be checked when the single instance starts.
-u user # Custom program runs in whose capacity.


Related articles: