This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge

A lifelong learner, practitioner, and sharer committed to the path of technology, an original blogger who is busy and sometimes lazy, and a teenager who is occasionally boring and sometimes humorous.

Welcome to search “Jge’s IT Journey” on wechat!

Process and Scheduled Task management in Linux

preface

A program is a static collection of executable machine code and data stored in an external storage medium (such as a hard disk), while a process is a computer program that executes dynamically in the CPU and memory. On Linux, one or more processes can be created after each program is started.

1. The ps command is used to view static process statistics

The ps command is the most commonly used tool for viewing processes in Linux. It is used to display a static snapshot containing complete information about each process running. You can run different commands to view process information.

  • A: Displays information about all processes on the current terminal, including processes of other users. When combined with the “x” option, all process information in the system is displayed.
  • U: Outputs process information in a user-oriented format.
  • X: Displays the process information of the current user on all terminals.
  • -e: Displays information about all processes in the system.
  • -l: Displays the process information in a long format.
  • -f: displays the process information in a complete format.

After the ps aux command is executed, the process information is displayed in a simple list.

In the following output, the first behavior is the title of the list, and the meanings of each field are described as follows:

  • USER: indicates the name of the USER account used to start the process.
  • PID: Unique process ID in the current system.
  • %CPU: Indicates the CPU usage percentage.
  • %MEM: Indicates the memory usage percentage.
  • VSZ: indicates the size of the virtual memory swap space.
  • RSS: The size of the resident memory physical memory.
  • TTY: indicates the terminal on which the process is running. “?” Indicates that the terminal is unknown or not required.
  • STAT: Displays the current status of the process, S: sleep, R: running, Z: dead, < : high priority, N: low priority, S: parent process, + : foreground process.
  • START: indicates the time to START the process.
  • TIME: indicates the CPU TIME used by the process.
  • COMMAND: indicates the name of the COMMAND used to start the process.
[root@localhost ~]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.1 0.1 19364 1540? Ss 12:19 0:01 /sbin/init root 2 0.0 0.0 0 0? S 12:19 0:00 [kthreadd] root 3 0.0 0.0 00? S 12:19 0:00 [migration/0] root 4 0.0 0.0 00? S 12:19 0:00 [ksoftirqd/0] root 5 0.0 0.0 00? S 12:19 0:00 [migration/0] root 6 0.0 0.0 00? S 12:19 0:00 [watchdog/0] root 7 0.0 0.0 00? S 12:19 0:00 [migration/1] root 8 0.0 0.0 00? S 12:19 0:00 [migration/1] root 9 0.0 0.0 00? S 12:19 0:00 [ksoftirqd/1] root 10 0.0 0.0 00? S 12:19 0:00 [watchdog/1] root 11 0.0 0.0 00? S 12:19 0:00 [events/0] root 12 0.0 0.0 00? S 12:19 0:00 [events/1] root 13 0.0 0.0 00? S 12:19 0:00 [cgroup] root 14 0.0 0.0 00? S 12:19 0:00 [khelper] root 15 0.0 0.0 00? S 12:19 0:00 [netns] root 16 0.0 0.0 00? S 12:19 0:00 [async/ MGR] root 17 0.0 0.0 00? S 12:19 0:00 [PM] root 18 0.0 0.0 00? S 12:19 0:00 [sync_supers] root 19 0.0 0.0 00? S 12:19 0:00 [bdi-default] root 20 0.0 0.0 00? S 12:19 0:00 [kintegrityd/0] root 21 0.0 0.0 00? S 12:19 0:00 [kintegrityd/1]Copy the code

If you run the ps -elf command, the process information in the system is displayed in a long format with a large amount of information displayed. The output also includes the PPID column, which represents the PID number of the corresponding process’s parent process.

[root@localhost ~]# ps -elf F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD 4 S root 1 0 0 80 0 - 4841 poll_s When? 00:00:01 /sbin/init 1 S root 2 0 0 80 0 - 0 kthrea 12:19 ? 00:00:00 [kthreadd] 1 S root 3 2 0 -40 - - 0 migrat 12:19 ? 00:00:00 [migration/0] 1 S root 4 2 0 80 0 - 0 ksofti 12:19 ? 00:00:00 [ksoftirqd/0] 1 S root 5 2 0 -40 - - 0 cpu_st 12:19 ? 00:00:00 [migration/0] 5 S root 6 2 0 -40 - - 0 watchd 12:19 ? 00:00:00 [watchdog/0] 1 S root 7 2 0 -40 - - 0 migrat 12:19 ? 00:00:00 [migration/1] 1 S root 8 2 0 -40 - - 0 cpu_st 12:19 ? 00:00:00 [migration/1] 1 S root 9 2 0 80 0 - 0 ksofti 12:19 ? 00:00:00 [ksoftirqd/1] 5 S root 10 2 0 -40 - - 0 watchd 12:19 ? 00:00:00 [watchdog/1]Copy the code

When you execute the ps command directly without any options, only the processes that are open in the current user session are displayed.

[root@localhost ~]# ps
 PID TTY          TIME CMD
2258 pts/1    00:00:00 bash
2280 pts/1    00:00:00 bash
2369 pts/1    00:00:00 ps
Copy the code

A large number of processes are running in the system. To query information about a process, you can run the pipeline command and grep command to filter the information.

/ root @ localhost ~ # ps aux | grep bash root 2239 0.0 108336 0.1 1772 PTS / 0 Ss + 12:21 0:00 / bin/bash root 2258 0.0 0.1 108336 1812 PTS /1 Ss 12:23 0:00 -bash root 2280 0.0 0.1 108336 1764 PTS /1 S 12:23 0:00 bash root 2753 0.0 0.0 103256 848  pts/1 S+ 13:41 0:00 grep bashCopy the code

2. Top command — View process dynamic information

The top command displays the process rank in a full-screen interactive interface and tracks the system resource usage, including CPU and memory, in a timely manner. By default, the command is refreshed every three seconds.

In the preceding command output, information about Tasks, CPU usage, memory usage, and Swap space is displayed in the beginning, and the process ranking is displayed in the lower part.

  • System task information: total: total number of processes. Running: Indicates the number of running processes. Sleeping: Indicates the number of dormant processes. Stopped: Number of processes stopped; Zomibe: Number of processes that are dead and unresponsive.
  • CPU usage: US: User usage. Sy: Kernel occupancy; Ni: Priority scheduling occupation; Id: indicates an idle CPU. Wa: I/O waiting to be occupied. Hi: Hardware interrupt occupation. Si: Software interrupt occupancy; St: Occupied by virtualization. Rt: Real-time change.
  • Memory usage Mem information: Total: indicates the total memory space. Used, used memory; Free, free memory; Buffers.
  • Swap space swap Occupied: total, total swap space. Used, used swap space; Free, free swap space; Cached.

On the full-screen operation screen of the top command, you can press P to sort the process list by CPU usage, M to sort the process list by memory usage, N to sort the process list by startup time, H to obtain online help information about the top program, and Q to exit the Top program.

3. Run the pgrep command to query process information

The pgrep command is used to query information about a specific process. Using the pgrep command, you can query the PID of a specific process based on the process name, user running the process, and terminal where the process is running.

Using the pgrep command, you can query only part of the process names. When using the -l option, you can output the corresponding process name, -u to query the process of a specific user, and -t to query the process running on a specific terminal.

[root@localhost ~]# pgrep -l "log"
1180 rsyslogd
Copy the code

4. Pstree command — queries the process tree

The pstree command outputs the tree structure of each process in the Linux system and determines the relationship between each process (parent and child processes). By default, the pstree command displays only the name of each process. If the -p option is used, the PID number, the user name, and the -a option are displayed.

You can run the pstree-aup command to view the process tree of the current system, including the PID number, user name, and complete command information of each process.

/ root @ localhost ~ # pstree - aup init, 1 ├ ─ NetworkManager, 1249 -- pid - file = / var/run/NetworkManager NetworkManager. Pid │ ├─ DhClient, 2909-d-4 -sf /usr/libexec/nm-dhcp-client.action -pf /var/run/dhclient-eth1.pD-lf... │ └ ─ {NetworkManager}, 1300 ├ ─ abrtd, 1570 ├ ─ acpid, 1328 ├ ─ atd, 1589 ├ ─ auditd, 1155 │ └ ─ {auditd}, 1156 ├ ─ automount, 1411 │ ├─{automount},1413 │ ├─{automount}, 1414 │ ├─{automount}, 1414 │ ├─{automount}, 1414 │ ├─{automount}, 1414 │ ├─{automount}, 1420 ├ ─ bonobo - activati, 1945 - ac - activate - ior - output - fd = 18 │ └ ─ {bonobo - activat}, 1946 ├ ─ certmonger, 1601 - S - p The/var/run/certmonger pid ├ ─ clock applet, 2192 - the oaf - activate - iid = OAFIID: GNOME_ClockApplet_Factory - oaf - ior - fd = 34 │ ├─{console-kit-dae},1673 │ ├─{console-kit-da},1674 │ ├─{console-kit-da},1674 │ ├─{console-kit-da},1675 │ ├─ ├─{console-kit-da},1673 │ ├─ ├─{console-kit-da},1674 │ ├─{console-kit-da},1675 │ ├─ Bass exercises ├ ─ kit - da} {the console -, 1676Copy the code

5. At one-off task setting

The scheduled task configured by using the at command can be executed only once at the specified point in time. The prerequisite is that the corresponding system service ATD is running. Note that the scheduled execution time and date must be arranged after the current system time; otherwise, the scheduled task cannot be set correctly.

Set a one-time project task, in at the command line, in turn, specify plans to perform a task time, date as a parameter (if only to specify time represents the time of day, if only the current time) of the specified date indicates the date, confirm and brought into the “at” > prompt mission editor interface, each row set an executive command, can set up multiple statements in turn, Finally, press Ctrl+D to submit the task. The set command operations will be executed sequentially at the scheduled point in time.

[root@localhost ~]# date 2016 06月 17日 Friday 15:01:40 CST [root@localhost ~]# at 15:05 2016-06-17 at> cat /proc/cpuinfo > /tmp/ps.root at> <EOT> job 2 at 2016-06-17 15:05 [root@localhost ~]# cat /tmp/ps.root processor : 0 vendor_id : GenuineIntel CPU Family: 6 Model: 61 Model Name: Intel(R) Core(TM) i5-5200U CPU @ 2.20ghz Stepping: 4 CPU MHz: 2194.915 Cache size: 3072 KB FPU: yes Fpu_exception: yes CPUID level: 20 WP: yes flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon xtopology tsc_reliable nonstop_tsc unfair_spinlock pni pclmulqdq ssse3 fma cx16 pcid  sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm 3dnowprefetch arat fsgsbase smep Bogomips: 4389.83 CLFlush size: 64 Cache_alignment: 64 Address sizes: 40 bits physical, 48 bits virtual power management: processor : 1 vendor_id : GenuineIntel cpu family : 6 model : 61 Model name: Intel(R) Core(TM) i5-5200U CPU @ 2.20ghz Stepping: 4 CPU MHz: 2194.915 Cache Size: 3072 KB FPU: yes fpu_exception : yes cpuid level : 20 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon xtopology tsc_reliable nonstop_tsc unfair_spinlock pni pclmulqdq ssse3 fma cx16 pcid  sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm 3dnowprefetch arat fsgsbase smep Bogomips: 4389.83 CLFlush size: 64 Cache_alignment: 64 Address sizes: 40 bits physical, 48 bits virtual power management: You have mail in /var/spool/mail/rootCopy the code

Automatically shuts down the current system at 15:10 on the day.

[root@localhost ~]# at 15:10
at> cat /proc/cpuinfo
at> <EOT>
job 3 at 2016-06-17 15:10
Copy the code

You can run the ATq command to query scheduled tasks that have been set but have not been executed.

[root@localhost ~]# atq
3  2016-06-17 15:10 a root
Copy the code

To delete an AT task with a specified number, run the atrm command. The deleted AT task will not be executed and will not be displayed in the result of the ATQ command. However, a task that has been executed cannot be deleted.

[root@localhost ~]# at 15:16
at> cat /proc/cpuinfo
at> <EOT>
job 4 at 2016-06-17 15:16
[root@localhost ~]# atrm 4
[root@localhost ~]# atq
Copy the code

Set periodic crontab tasks

Using the crontab command, scheduled tasks can be executed repeatedly at a preset period, which greatly reduces the operation of setting repetitive system administration tasks. A prerequisite for periodic tasks is that the system service Crond is already running.

Crontab configuration file and directory

Crond sets scheduled tasks through multiple directories and files, and different types of tasks are set by different configuration files.

The /etc/crontab file contains tasks required for maintaining the Linux operating system and is automatically set by the Linux operating system and related programs during installation. You are not advised to manually modify the file. This file contains operations for setting variables such as shell environment, executable path, and directories for tasks to be performed hourly, daily, weekly, and monthly.

/ root @ localhost ~ # cat/etc/crontab SHELL = / bin/bash SHELL environment plan task set PATH = / sbin: / bin: / usr/sbin, / usr/bin to define executable commands and PATH of the program MAILTO=root Send the task output to the mailbox of the specified user HOME=/ HOME directory used to execute the scheduled task # For details see man 4 crontabs # Example of job definition: #. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - minute (0 -) of 59 minutes. # | -- -- -- -- -- -- -- -- -- -- -- -- -- hour (0-23) hours # | |. -- -- -- -- -- -- -- -- -- -- day of the month (1-31) day # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... Month # | | | |. - day of week (0 to 6) (7) Sunday = 0 or or sun, mon, tue, wed and thu, fri, sat week # | | | | | # * * * * * user name command to be executedCopy the code

/var/spool/cron — the directory where configuration files for user cron tasks are stored

The preset cron scheduled task is saved to /var/spool/cron/ with the same file name as the user name.

[root@localhost ~]# ls -l /var/spool/cron/Total usage 0Copy the code

The crond daemon automatically checks for changes in the /etc/crontab file, /etc/cron.d/ directory, and /var/spool/cron directory. If any configuration changes are detected, they are loaded into memory. So when a crontab file changes, you don’t need to restart the crond daemon to make the Settings take effect.

Use the crontab command to manage scheduled tasks for users

You can run the crontab command to set the periodic scheduled task list. Different options can be used to manage different scheduled tasks.

  • -e: Edit the scheduled task list.
  • -u: specifies the user whose scheduled tasks are managed. By default, only the root user has the permission to edit and delete the scheduled tasks of other users.
  • -l: displays scheduled tasks in a list.
  • -r: deletes the scheduled task list.

* indicates any time in the value range. The command operations specified in the crontab task configuration record will be executed when minute, hour, day, month, and week are all met.

In addition to “*”, you can also use the minus sign “-“, comma “, “, slash “/” and numbers to form an expression to express a more complex time relationship.

“-” : indicates a continuous time range

“, “: indicates the discontinuous range of an interval

/ : indicates the specified interval frequency. For example, */5 indicates every five days.

Crontab -e command: you can set your own scheduled task.

[root@localhost ~]# crontab -e
01 * * * * /sbin/service httpd start
02 * * * * /sbin/service httpd stop
Copy the code

Run the crontab -l command to view the list of scheduled tasks of the current user. (-u: you can view the scheduled tasks of other users)

[root@localhost ~]# crontab -l
01 * * * * /sbin/service httpd start
02 * * * * /sbin/service httpd stop
Copy the code

Run the crontab -r command to delete the scheduled task list of the user.

[root@localhost ~]# crontab -r
[root@localhost ~]# crontab -l
no crontab for root
Copy the code

Recommended reading

99% of Linux operation and maintenance engineers must master the command and use

Common commands of the Oracle database in Linux

Common commands of the vi/vim editor in Linux

Install and manage programs in Linux (basic process of package encapsulation, RPM command, source code compilation and installation)

Manage accounts and rights in Linux

Linux disk and file system management

In this paper, to the end.


Original is not easy, if you think this article is a little useful to you, please give me a like, comment or forward for this article, because this will be my power to output more quality articles, thanks!

By the way, dig friends remember to give me a free attention yo! In case you get lost and you can’t find me next time.

See you next time!