Processes and Scheduled Tasks

Source: Internet
Author: User
Tags terminates time and date cpu usage

I. Overview of the process and the differences between Linux and Windows processes

?

    在**Linux**内核中,描述一个进程主要是task_struct,一个称为进程描述符的数据结构。这个数据结构很庞大,包含了内核管理一个进程所需的所有信息,描述了一个正在执行的进程,包括进程ID,它打开的文件,进程的地址空间,挂起的信号,进程的状态等等其他信息。    Linux的线程机制和Windows等其他操作系统的很不一样,Linux中没有为线程设置专门的数据结构,也没有专门的线程调度算法,在Linux内核看来,线程就是一个进程,只是一个和其他进程共享资源的特殊进程而已。    在Linux中,创建线程时,和创建普通的进程类似,每个线程都拥有自个的进程描述符task_struct,只是在调用clone()的时候需要传递一些参数标志来指明共享的资源。1所示。

?


?

In other operating systems, such as windows, the process has a process descriptor that describes shared resources such as address space and open files, with pointers to different threads in the process.
These threads do not have a process descriptor and describe only a small amount of unique resources, so they are lightweight. These threads also share the resources of the process. As shown in 2.

?

Ii. status and priority of the process

?
1. Status of the process

Linux kernel: Preemptive multi-tasking
Process Type:
Daemon: daemon, process initiated during system boot, and terminal-independent process
Foreground process: terminal-related, process initiated through terminal
Note : The two can be converted to each other
Process Status:
Operating state: Running
Ready state: Readiness
Sleep state:
Interruptible: interruptable
Non-disruptive: uninterruptable
Stop state: Stopped, paused in memory, but not dispatched unless manually started
Zombie State: Zombie, end process, child process does not close before parent process ends

?
2. Priority of the process

Process priority:
System priority: The smaller the number, the higher the priority
0-139 (centos4,5) each has 140 run queues and an expiration queue
0-98,99 (CENTOS6)
Real-time Priority: 99-0 Value Maximum priority
Nice value: 20 to 19, corresponding to system priority 100-139 or 99
Big O: The relationship between time complexity, timing, and scale
O (1), O (Logn), O (n) linear, O (n^2) parabolic, O (2^n)

?

Third, view the process PS, top command

?

The PS command is used to report the process status of the current system. You can break and delete unnecessary programs with the KILL command at any time. The PS command is the most basic and powerful process view command that can be used to determine which processes are running and running, whether the process is complete, if the process is dead, which processes are consuming too much resources, and so on, and most of the information can be obtained by executing the command.

BSD方式   a 所有用户在所有终端上运行的前台进程    x 当前用户运行的所有进程(包括前台进程和后台daemon)   ax 所有用户运行的所有进程  ** aux** 所有用户运行的所有进程,结果中显示用户名    f 显示父子进程关系   -C 用进程名进行过滤   axo配合    o 自定义输出列        %cpu        %mem        pid        uid        gid        cmd        comm        tty  显示终端名,如为?,表示daemon后台进程        ruser 命令的发起人        euser 命令的真正执行人        state            k 排序或--sort


Each circle has the following meanings:

?

User: Current users of the process; PID: There is no doubt that the process ID of the abbreviation, also on the progress number;
CPU utilization of the%CPU process; the memory occupancy rate of the%MEM process;
The size of the virtual memory used by the VSZ process, or the size of the resident set used by the RSS process;
TTY associated with a process terminal (TTY);
STAT Check Status: The process state is represented by a character, such as R (running is running or ready to run), S (sleeping Sleep), I (Idle idle), Z (Zombie), D (non-disruptive sleep, usually I/O), P (Waiting for swap pages), W (swap out, Indicates the current page is not in memory), N (Low priority Task) T (terminate termination), W has no resident pages
Start (Process start time and date); Time;(the total CPU times used by the process);

?
When the server is attacked more than some inexplicable other process, it is necessary to kill the process in one row, with the kill or Killall command "
Usage: Kill
-0 Nothing, can be used to simply determine whether the process is running.
-1-hup: Forcing the process to reread the configuration file
-2-int: Aborts a running process, equivalent to CTRL + C
-3-quit: Equivalent to Ctrl+\
-9-kill: Forces a running process to be killed (use cautiously)
-15-term: Terminates a running process (default) \

Killall Process Life
The difference: The use of the KILL command when the PID, and the PID will change, but we generally only know the startup process of the program or command name, so we should first use PS combined with grep to find the corresponding PID. Killall can simplify this process by directly following the killall with the corresponding command, which kills all processes that use this command name or program name.

?
I'll use the ping command for an example.

?
Review the process and see the ping process.


?
Kill with Kill


The ping process is interrupted at this time
?

The top command can view the overall operation of the system in real time, and is a practical tool to synthesize the performance and operation information of the multi-party information monitoring system. The interactive interface provided by the top command can be managed with a hotkey.
-B: Operation in batch mode;
-C: Displays the complete ruling order;
-D: Screen refresh interval time;
-I: ignore the failure process;
-S: Secrecy mode;
-S: Cumulative mode;
-i< time;: Set interval time;
-u< user name;: Specify user name;
-p< process number;: Specify the process;
Number of-n<;: The number of times the loop is displayed.

Some of the interactive commands that you can use during the top command execution. These commands are single-letter, and some of these commands may be masked if the-s option is used on the command line.

H: Display the help screen, give some brief summary of the command;
K: terminates a process;
I: Ignore idle and zombie process, this is a switch-type command;
Q: Exit the program;
R: Rearrange the priority level of a process;
S: Switch to cumulative mode;
S: Change the delay time between two refreshes (in units of s), and if there are decimals, convert to Ms. Enter 0 value, the system will be constantly refreshed, the default value is 5s;
F or F: Add or remove items from the current display;
O or O: Change the order of the displayed items;
L: Toggle display average load and start time information;
M: Toggle display memory information;
T: Toggle display of process and CPU status information;
C: Toggle display command name and full command line;
M: Sort According to the size of the resident memory;
P: Sort according to CPU usage percent size;
T: Sort by Time/cumulative time;
W: Writes the current settings to the ~/.TOPRC file.

?

 ● top - 20:50:09[当前系统时间],  ● up 11:26[系统已经运行了11.26],  ● 4 user[个用户当前登录],  ● load average: 0.00,0.00,0.00[系统负载,即任务队列的平均长度] 1分 5分 10分  ● Tasks: 178 total[总进程数],  ● 1 running[正在运行的进程数],  ● 177 sleeping[睡眠的进程数],  ● 0 stopped[停止的进程数],  ● 0 zombie[冻结进程数],  ● Cpu(s): 0.0%us[用户空间占用CPU百分比],  ● 0.2%sy[内核空间占用CPU百分比],  ● 0.0%ni[用户进程空间内改变过优先级的进程占用CPU百分比],  ● 99.5%id[空闲CPU百分比], 0.0%wa[等待输入输出的CPU时间百分比],  ● 0.0%hi[硬件CPU中断占用百分比],  ● 0.0%si[软中断占用百分比],  ● Mem:  2038352k  total[物理内存总量],  ● 803624k used[使用的物理内存总量],  ● 1234728kfree[空闲内存总量],  ● 59052k buffers[用作内核缓存的内存量]  ● Swap: 4194300k total[交换区总量]  ● 0k used[使用的交换区总量],  ● 4194300k free[空闲交换区总量],  ● 422760k cached[缓冲的交换区总量],ps:以上数据与不同是因为此段参数详解来自我当时笔记里的,除了数据不同其余含义一样。
Iv. Scheduled Tasks

?
1, the Planning task summary;
?
Planning a task is a task that performs the planned work at the agreed time, which is the surface meaning. In Linux, we often use the Crond server to do this work. The cron server can perform specific work based on the time specified in the configuration file. For example, we can contract in the configuration file every morning 4 points, the HTTPD server restart, this is a scheduled task;

?

2. Recurring Task Plan Cron

周期性任务计划:cron 相关的程序包: cronie: 主程序包,提供crond守护进程及相关辅助工具 cronie-anacron:cronie的补充程序,用于监控cronie任 务执行状况,如cronie中的任务在过去该运行的时间点未能正常 运行,则anacron会随后启动一次此任务 crontabs:包含CentOS提供系统维护任务

?

3, Crond Start-up and installation

In general, Cron is installed by default and we want to see if the package is installed:
[[Email protected] ~] #rpm-qa |grep cron
CentOS 7:systemctl Status Crond
CentOS 6:service crond Status

?

Schedule recurring tasks to be submitted to Crond and run automatically at specified times
System Cron Task: system maintenance Job
/etc/crontab
User Cron task: crontab command
LOG:/var/log/cron

?

* represents all
Cron Configure the writing format for scheduled tasks
Minute hour Sun Moon Week [user name] command

4. Let the task plan boot from start

例如:晚上9点10分运行echo命令  10 21 * * * centos /bin/echo "Howdy!" 例如,每天的8点5分执行haha.sh “5 8 * * * /opt/./haha.sh”
*如果想自动开机运行,请用下面的办法;*[[email protected] ~]# chkconfig --levels 35 crond on全局性文件是 /etc/crontab ,每个用户也有自己的cron配置文件,我们可以通过crontab -e 来编辑它;crontab命令: crontab [-u user] [-l | -r | -e] [-i] -l: 列出所有任务; -e: 编辑任务; -r: 移除所有任务; -i:同-r一同使用,以交互式模式移除指定任务 -u user: 仅root可运行,指定用户管理cron任务

For cron tasks,% has a special purpose, if you want to use% in the command, you need to escape, put% in single quotes, you can not escape

?

V. Expansion of knowledge (how to add a script to a system service)

Let's say, for example, how to add a shell script as a system service and follow the system boot up:

You can see that there are a lot of files under "/etc/rc.d/init.d", each one can see the content, in fact, some shell scripts.
The startup of the system service is implemented through the script file in "/etc/rc.d/init.d". We can also write a script of our own to put here.
The contents of the script file are also very simple, similar to this (for example, a name called "Hahad"):
. /etc/init.d/functions
Start () {
echo "Starting my Process"
Cd/opt
./haha.sh
}
Stop () {
Killall haha.sh
echo "stoped"
}
After you've written the script file, it's not over yet, so go ahead and complete the following steps:

  • chmod +x Hahad #增加执行权限
    Chkconfig--add Hahad #把hahad添加到系统服务列表
    Chkconfig Hahad on #设定hahad的开关 (on/off)
    Chkconfig--list Hahad #就可以看到已经注册了hahad的服务 *

That's when all the work is done.

Processes and Scheduled Tasks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.