First, crond (scheduled task management)
1.cRond is the command that Linux uses to execute programs on a regular basis. This task dispatch command is started by default when the operating system is installed. The Crond command periodically checks to see if there is any work to be done and the work will be performed automatically if there is work to be done. Linux task scheduling is mainly divided into the following two categories:
1) What the system does: the work to be performed by the system periodically, such as backing up system data, cleaning up the cache
2) Work performed by a person: a user's regular work, such as checking the mail server for new letters every 10 minutes, which can be set by each user
2.crontab // allow the user to execute the program at a fixed time or at regular intervals
(1) How to use: crontab-l|-r|-e|-i [username]
(2) Description:
Usename: Refers to setting the time table for the specified user (provided that you have permission to specify a time table for others). If you do not use the-u user, it means setting your own schedule.
(3) Parameters:
-E: Executes the text editor to set the time schedule (the default text editor is VI) crontabl-e
-R: Delete the current schedule table crontabl-r
-L: Lists the current schedule table crontabl-l
-I: Prompt crontabl-ri before deleting the schedule table
(4)Time Schedule Table Format: F1 F2 F3 f4 f5 program
Note: F1: minutes, F2: Hours, F3: Date, F4: Month, F5:, Program: Programs to execute.
If F1 (F2 ...):"*" ---> Per minute (hours ...) ) to execute the program
"*/n" ----> every n minutes (...) Time interval to execute once
"a, B, C,... "---> A, b, C,... Minutes (...) To perform a
(5) The user can also store all the settings in the file file, using the crontab file to set the time schedule.
How to use: edit a file cronfile with VI and enter a well-formed time table in this file. When the edits are complete, save and exit.
$: crontab cronfile
Second, daemon (no control terminal)
Daemons, also known as "daemon Processes". A daemon is a special process that runs in the background. It is independent of the control terminal and periodically performs some sort of task or waits for certain occurrences to be handled, i.e. not associated with any control terminal.
The daemon usually uses the name ending with D, which indicates Daemon.
Parameter A: Column The current user's process + all other users ' processes
Parameter x: A process with control terminal + all non-control terminal processes
Parameter J: Lists information related to job control.
Example: PS axj | grep-e ' d$ '//Find all processes ending in D
Tpgid:-1--no process to control the terminal (daemon)
Command:[]---> Kernel process
Creating Daemons :
1. Call Umask to set the file mode creation shield Word to 0. (umask (0); )
2. Call fork, the parent process exits (exit).
Reason: 1) If the daemon is started as a simple shell command, the parent process terminates so that the shell considers that the command has been executed.
2) Ensure that the child process is not a process group leader process.
3. Call Setsid to create a new session. (Setsid (); )
Setsid causes: 1) The calling process becomes the first process of a new session.
2) The calling process becomes the leader process of a process group.
3) The calling process does not control the terminal. (Fork once again to ensure that the daemon process does not open the TTY device afterwards)
4. Change the current working directory to the root directory. (ChDir)
5. Close the file descriptor that is not required (Input output error).
6. Other: Ignore the SIGCHLD signal.
Instance:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #define maxfile 3void my_daemon () {Int i=0;int fd;struct sigaction sa;sa.sa_handler = sig_ign;sa.sa_flags = 0;sigemptyset (&sa.sa_mask); umask (0);p id_t id=fork (); id < 0) {perror ("fork"); exit (1);} Else if (id > 0)//father{exit (0);} Setsid (); if (id < 0) {perror ("fork"); exit (1);} Else if (id > 0)//father{exit (0);} if (ChDir ("/") < 0) {perror ("chdir"); exit (1);} for (int i=0;i<maxfile;i++) {close (i);} Signal (sigchld,sig_ign); close (FD);} Int main () {File *fp;time_t t;my_daemon (); while (1) {sleep (6); if ( (Fp=fopen ("./test.log"), "a")) ( >= 0) {int i=0;t = time (0); fprintf (fp, "[%d]hello friends \n ", i++); fclose (FP); }}return 0;}
This article is from the "Flower Open Shore" blog, please be sure to keep this source http://zxtong.blog.51cto.com/10697148/1788269
Crond and Guardian Process