[Linux] Supplement: Process management, manual control of processes, and scheduled tasks

Table of Contents

1. Manually start the process

1. Understand foreground startup and background startup

2. How to complete the switch between foreground startup and background startup

3. Complete execution of multiple tasks in parallel

4. End the process

1.kill

2.killall

2. pkill

2. Planning tasks

1. at one-time planned task

2. Practical operation

2. Periodic planning tasks

1. About setting the configuration file and format of periodic tasks

2. Methods and commands for setting periodic tasks

Practical case: Create a file name at the same time every minute at 14 o’clock


1. Manually start the process

1. Understand foreground startup and background startup

Foreground startup: Start through the terminal, and it will occupy the terminal after startup. The terminal cannot do other things.

Background startup: It can be started through the terminal, but after startup, it will run in the background (release the terminal), and the terminal can do other things.

Both can be switched

2. How to complete the switch between foreground startup and background startup

Use “command & amp;” to put the process into the background for execution

##Command dd & amp; put it into the background for execution and give it a process pid number
[root@localhost opt]#dd if=/dev/zero of=/opt/bigfile & amp;
[1] 13991


##View the current background task status: jobs
[root@localhost opt]#jobs
[1] + Running dd if=/dev/zero of=/opt/bigfile & amp;



##View the current background task status and pid number: jobs -l
[root@localhost opt]#jobs -l
[1] + 13991 running dd if=/dev/zero of=/opt/bigfile & amp;


##Put background tasks to the foreground to run: fg + task number
[root@localhost opt]#fg 1
dd if=/dev/zero of=/opt/bigfile
^Z
[1] + stopped dd if=/dev/zero of=/opt/bigfile

###ctrl + z will stop and suspend the task in the foreground. It is stopped but still there.


##Look at the status difference of tasks
[root@localhost opt]#jobs
[1] + stopped dd if=/dev/zero of=/opt/bigfile


##Start the pending task again: bg + task number
[root@localhost opt]#bg 1
[1] + dd if=/dev/zero of=/opt/bigfile &


##Look at the status again
[root@localhost opt]#jobs
[1] + Running dd if=/dev/zero of=/opt/bigfile & amp;


3. Complete execution of multiple tasks in parallel

For example, if you want to ping three hosts at the same time

ping 127.0.0.1 & amp; ping www.baidu.com & amp; ping 192.168.20.10 & amp;

4. End process

1, kill

No. Signal name Description
0 EXIT This message is received when the program exits.
1 HUP This signal causes the process to shut down immediately. Then re-read the configuration file and restart
2 INT Program abort signal, used to terminate the foreground process. Equivalent to the output Ctrl + C shortcut key
3 QUIT Exit
8 FPE Emitted when a fatal arithmetic error occurs. It includes not only floating point operation errors, but also all other arithmetic operation errors such as overflow and division by 0
9 KILL Use to end the program immediately. This signal cannot be blocked, processed or ignored. Generally used to forcefully terminate the process
14 ALRM Clock timing signal, which calculates actual time or clock time. The alarm function uses this signal
15 TERM Signal to terminate the process normally, the default signal of the kill command. If a problem has occurred in the process, then this signal cannot terminate the process normally. Only then will we try the SIGKILL signal, which is signal 9
18 CONT This signal allows a suspended process to resume execution. This signal cannot be blocked
19 STOP This signal can pause the foreground process, which is equivalent to entering Ctrl + Z shortcut key. This signal cannot be blocked

2, killall

The command no longer relies on PID to kill a single process, but uses the process name of the program to kill a class of processes, such as killing ping

Kill all ping-related processes: killall ping

2. pkill

 pkill [-t terminal number] process name
 
-U: Terminate the corresponding process according to the user name to which the process belongs
-t: Terminate the corresponding process according to the terminal where the process is located
-SIGNAL
-u uid: effective user, effective user
-U uid: real user, the person who actually initiated the command execution ##pkill -U zhangsan Clear the zhangsan user
-t terminal: process associated with the specified terminal
-l: Display process name (available with pgrep)
-a: Display the process name in full format (available with pgrep)
-P pid: Display the child processes of the specified process




w
#Use the w command to query the logged-in users of this machine
pkill -9 -t pts/1
#Force kill the process logging in from the pts/1 virtual terminal

2. Planned tasks

1. at one-time planned task

  • Provided by package at
  • Depends on the atd service and needs to be started to implement the at task
  • The at queue is stored in the /var/spool/at directory, and ubuntu is stored in the /var/spool/cron/atjobs directory.
  • When executing a task, the value of the PATH variable is consistent with the identity of the user currently defining the task.

2. Practical operation

##Format of one-time tasks
at [xx:xx] [xxx-xx-xx]

#The first step of interaction is to write the task content
Ordinary commands or scripts

##submit
ctrl+d


##Query (there is a disadvantage here, and you cannot view specific tasks)
atq


##Delete task
atrm task number

2. Periodic scheduled tasks

1. About setting the configuration file and format of periodic tasks

It is the /etc/crontab configuration file that indicates the writing format of periodic tasks. This file is a global configuration file, and it can take effect globally after being written

When writing crontab, you need to pay attention to several formats:

1. None of the 6 options can be empty. If you are not sure, use * instead. Please note that the first item is minutes. You must think clearly about this. * means every minute, 0 means the hour. It is best to specify the time. , otherwise execute it once every minute and think clearly about the requirements;

2. The smallest unit is minutes, so precise seconds are not possible! !

3. When defining the time, pay attention to the difference between the day of the week and the date, which is easy to confuse;

4. When setting tasks, pay attention to using absolute paths for commands! ! !

2. Methods and commands for setting periodic tasks

##Method 1:
vim /etc/crontab


##Method Two:
crontab command

crontab -e #Create a periodic plan
crontab -l #View the current periodic plan
crontab -r #Delete scheduled tasks. Note that when deleting, you will delete them all at once! ! !
crontab -u username -e #Create periodic tasks for users
Options Function
< strong> -u username Used to set the crontab service of a certain user, such as “-u demo ” represents the crontab service of the device demo user. This option is generally run by the root user.
-e Edit the contents of a user’s crontab file. If no user is specified, it means editing the current user’s crontab file.
-l Display the crontab file content of a certain user. If the user is not specified, it means displaying the crontab file content of the current user.
-r

Note; if you delete it here, it will delete everything. It is recommended to use crontab -e to enter the editing page to delete a single item

Delete a user’s crontab file from /var/spool/cron. If no user is specified, the current user’s crontab file will be deleted by default.

-i Give a confirmation prompt when deleting the user’s crontab file.

Practical case: Create a file with the same name every minute at 14 o’clock

[root@localhost opt]#crontab -l

* 14 * * * /bin/touch /opt/`date + \%F_\%T`

Replenish:

The meaning of special characters
item Meaning Range
The first “*” Minute of the hour 0~59
The second “*” The first day of the day Hours (hour) 0~23
The third “*” Day of the month (day) 1~31
The fourth “*” The month of the year (month) 1~12
The fifth “*” Day of the week (week) 0~7 (0 and 7 both represent Sunday)
Meanings of special characters
Special symbols Meaning
* (asterisk) represents any time. For example, the first “*” means that it will be executed every minute for an hour.
, (comma) represents discontinuous time. For example, “0 8, 12, 16*** command” means that the command is executed at 8:00, 12:00, and 16:00 every day.
– (middle bar) represents a continuous time range. For example, “0 5 ** 1-6 command” means executing the command at 5:00 am from Monday to Saturday.
/ (forward slash) represents how often it is executed. For example, “*/10**** command” means executing the command every 10 minutes.

Extended exercises:

1. The 1st, 15th and 30th of each month

0 0 1,15,30 * *

2. 12 noon – 16:00

0 12-16 * * *

3. Execute once every half hour

30 * * * *

4. Execute the script every two minutes

*/2 * * * *

5. Every 2 hours between 8:00 and 18:00

0 8-18/2 * * *

6. Every hour of every 3 days

0 0 */3 * *

7. Execution on the 1st, 5th, 15th and 30th of each month from 3 to 6 pm

0 15-16 1,5,15,30 * *

8. Execute every 5 minutes every 10 days from January to May in the morning and 10-45 minutes from 10 p.m.

10-45/5 10,22 */10 1-5 *

9. Executed every Friday at 8pm

0 20 * * 5

8. Execute once every Sunday at 2:30 am

30 2 * * 0

30 2 * * 7

3. Add something more

In practice, the configuration file /etc/crontab file has been modified, and scheduled tasks can also be set, but

① Need to follow the format exactly and specify a user

②You cannot use the command crontab -l to view tasks

So it is recommended not to use

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skills treek8s package management (helm)Installation helm16964 people are learning the system