Several ways of running in the background of Linux (summary)

Several ways of running in the background of Linux (summary)

1. nohup

Run the program in a way that ignores the hang-up signal

Additional Notes
The nohup command can run the program in a way that ignores the hang-up signal, and the output information of the program being run will not be displayed to the terminal.
Regardless of whether you redirect the output of the nohup command to a terminal, 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.
If no file can be created or opened for appending, the command specified by the command parameter cannot be invoked.
If standard error is a terminal, then all output of the specified command written to standard error is redirected to the same file descriptor as standard output.

Simple example:

nohup command &

Specifying Output Instances

nohup command > myout.file 2>&1 &

Other related commands
ctrl + z #You can put a command being executed in the foreground into the background and put it in a paused state.
fg #Switch background tasks to foreground execution
bg #Change a command that is paused in the background to continue executing in the background. If there are multiple commands in the background, you can use bg %jobnumber to call out the selected command.
jobs #Check the status of background running. The jobs -l option can display the PID of all tasks.
ps -ef | grep command or ps aux | grep command #View the process
kill -9 process id #kill the corresponding process,

More advanced usage is as follows:

ps aux | grep command | grep -v grep | awk '{print $1}' | xargs kill -9 #This means getting the process ID directly through command and killing it directly

2. screen

For command line terminal switching

a. Session recovery As long as the Screen itself is not terminated, the sessions running inside it can be recovered. This is especially useful for users logging in remotely - even if the network connection is lost,
Users also do not lose control of already opened command-line sessions. Simply log in to the host again and execute screen -r to resume the session.
Similarly, when you leave temporarily, you can also execute the detach command to suspend the screen (switch to the background) while ensuring that the program inside runs normally.

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

c. Installation of screen

yum install -y screen

Basic syntax
screen -AmRvx -[ls -wipe][-d <job name>][-h <number of lines>][-r <job name>][-s ][-S <job name>]

Options
-A Resize all windows to the size of the current terminal.
-d <job name> Take the specified screen job offline.
-h <number of lines> specifies the number of buffer lines for the window.
-m Forces the creation of a new screen job even if one is already in progress.
-r <job name> Restore the offline screen job.
-R Try to recover offline jobs first. If the offline job cannot be found, a new screen job is created.
-s specifies the shell to be executed when creating a new window.
-S <job name> specifies the name of the screen job.
-v Display version information.
-x Restore the previously offline screen job.
-ls or --list displays all current screen jobs.
-wipe checks all current screen jobs and deletes those that are no longer usable.

Common screen parameters
screen -S yourname -> create a new session called yourname
screen -ls -> List all current sessions
screen -r yourname -> Return to yourname session
screen -d yourname -> remotely detach a session
screen -d -r yourname -> End the current session and return to the yourname session

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

Ctrl + a ? -> Show all key binding information
Ctrl + ac -> Create a new window running a shell and switch to it
Ctrl + an -> Next, switch to the next window
Ctrl + ap -> Previous, switch to the previous window
Ctrl + a 0..9 -> switch to window 0..9
Ctrl+a [Space] -> Switch from window 0 to window 9 in sequence
Ctrl + a Ctrl + a -> Switch between the two most recently used windows
Ctrl + ax -> Lock the current window, and unlock it with the user password
Ctrl + ad -> 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 the screen. At this time, in the screen session, the process running in each window (whether foreground/background) continues to execute, and even logout will not affect it.
Ctrl + az -> put the current session into the background and use the fg command of the shell to go back.
Ctrl + aw -> Show all windows list
Ctrl + at -> time, display the current time and system load
Ctrl + ak -> kill window, force close the current window
Ctrl + a -> Enter copy mode. In copy mode, you can scroll back, search, and copy just like using vi
Cb Backward, PageUp
Cf Forward, PageDown
H (uppercase) High, move the cursor to the upper left corner
L Low, move the cursor to the lower left corner
0 Move to the beginning of the line
$ End of line
w forward one word, move forward in units of words
b backward one word, move backward one word
The first press of Space marks the starting point of the area, and the second press marks the end point.
Esc ends copy mode
Ctrl + a ] -> paste, paste the content just selected in copy mode

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

3. Daemonize tool

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

-a #Mount to output file instead of flushing the default value. Applies only if -e and/or -o is specified.
-e # Redirect the output standard error to the specified file, replacing /dev/null
-o #Redirect the output standard to the specified file, replacing /dev/null
-E name=value #Add environment variables to the background program. This parameter type must be in the name=value format. Parameters 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 a single instance is started.
-u user #Customize who the program runs as.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to install Redis in Linux, run it in the background, and set up the system to start automatically
  • How to view and close background running programs in Linux
  • Linux nohup to run programs in the background and view them (nohup and &)
  • How to start jar package and run it in the background in Linux
  • How to view and terminate running background programs in Linux
  • A brief analysis of the examples and differences of using nohup and screen to run background tasks in Linux
  • Several ways to run Python programs in the Linux background
  • How to run springboot in the background of linux
  • Linux background running node service command method
  • Solve the problem of python nohup linux background running output
  • Summary of methods for reliably running processes in the background in Linux
  • Detailed explanation of Linux background operation and shutdown, view background tasks
  • How to run Linux commands in the background

<<:  MYSQL transaction tutorial Yii2.0 merchant withdrawal function

>>:  WeChat applet implementation anchor positioning function example

Recommend

JS implements simple calendar effect

This article shares the specific code of JS to ac...

Mybatis statistics of the execution time of each SQL statement

background I am often asked about database transa...

Solve the problem of inconsistent MySQL storage time

After obtaining the system time using Java and st...

MySQL high availability solution MMM (MySQL multi-master replication manager)

1. Introduction to MMM: MMM stands for Multi-Mast...

CSS3 sample code to achieve element arc motion

How to use CSS to control the arc movement of ele...

Vue's Render function

Table of contents 1. Nodes, trees, and virtual DO...

Example code of the spread operator and its application in JavaScript

The spread operator allows an expression to be ex...

Detailed explanation of Vue router routing

Table of contents 1. Basic use 2. Several points ...

An analysis of div+float, a very important concept in website design

In website construction, you will always encounter...

MYSQL slow query and log settings and testing

1. Introduction By enabling the slow query log, M...

JavaScript counts the number of times a character appears

This article example shares the specific code of ...

HTML4.0 element default style arrangement

Copy code The code is as follows: html, address, ...