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

MySQL green version setting code and 1067 error details

MySQL green version setting code, and 1067 error ...

Native js to realize bouncing ball

On a whim, I wrote a case study of a small ball b...

Detailed explanation of several ways to create objects and object methods in js

This article is the second article about objects ...

WeChat applet calculator example

WeChat applet calculator example, for your refere...

Core skills that web front-end development engineers need to master

The content involved in Web front-end development...

How to quickly modify the table structure of MySQL table

Quickly modify the table structure of a MySQL tab...

Details after setting the iframe's src to about:blank

After setting the iframe's src to 'about:b...

Login interface implemented by html+css3

Achieve results First use HTML to build a basic f...

HTML table markup tutorial (22): row border color attribute BORDERCOLORLIGHT

Within rows, light border colors can be defined i...

Implementing a shopping cart with native JavaScript

This article shares the specific code of JavaScri...

Docker image import and export code examples

Import and export of Docker images This article i...

Detailed explanation of CSS label mode display property

The code looks like this: <!DOCTYPE html> &...

Detailed tutorial on running multiple Springboot with Docker

Docker runs multiple Springboot First: Port mappi...

MySQL multi-master and one-slave data backup method tutorial

Overview Operations on any one database are autom...

Example explanation of alarm function in Linux

Introduction to Linux alarm function Above code: ...