Linux system's web site at the time of operating state, we often need to maintain the site, for example, to view and respond to the remaining resources, log segmentation, data processing, to perform a specific task in a specific state, etc., these will need linux can automatically perform certain any task. This post describes how common linux automation tasks.
Achieve "Automation" has the following advantages:
Save manpower, a script is enough.
At night you can avoid the automatic execution site traffic peak, does not affect the efficiency of the site during the day.
By accurate, complete set of circumstances, it will not go wrong.
Of course, the most important thing is peace of mind, do not knock some frequent commands.
Boot should be our needs very often, we often need to boot automatically execute some commands to open the services, processes, etc., with which we do not have to enter the same bunch of commands when you switch.
Use chkconfig command can be set at different levels start to start a particular service or program.
Let me talk about the linux run level:
Level 0 means: showing off
Level 1 means: single-user mode
Level 2 indicates: no network connection multi-user command line mode
Level 3 means: multi-user command line mode with networking
Level 4 indicates: unavailable
Level 5 indicates: multi-user mode with GUI
Level 6, said: Restart
chkconfig command is as follows:
chkconfig --list // command has been set to open from the start list.
xxxd 0: off 1: off 2: on ... 6: off // result list, showing the xxxd service level at start of 2345 the situation will start automatically.
chkconfig --add xxxd // add a xxxd services to the task list
chkconfig [--level 1/2 /../ 6] xxxd on / off // set xxxd with service n state on / off, omitted in the 2345-level Open  within
chkconfig --del xxxd // task list xxxd service delete
Edit rc.d files
You can also edit files /etc/rc.d/ directory directly boot from the start. There are a lot of files in this directory, rcn.d is in a starting condition for the start of the next file the case n folders from Kai file rc, rc.sysinit, init.d all modules of the system or system settings [clip].
We use vim rc.local rc.local file editor to customize your own self-priming plan. Command is very simple, just like in the usual operation. Such as / usr / local / apache / bin / apachectl start showing boot from the start apache server.
at regular tasks realization
at a simple function is a simple timer task program, it can only be a one-time regular tasks, which are used as follows:
#at time // at plus time to start at the command
at> operation // input operations to be performed
at> Ctrl + D // Press Ctrl + D to exit the command editor
Common forms of its time as follows
at H: m tomorrow // H point m points the next day
at now n minutes / hours / days / weeks // in n minutes / hours / days / weeks +
at midnight at midnight // = - =
at H: m pm / am // in the morning / afternoon H point m points
We can also look at the current command in the / var / spool / at files. Also note that, linux default atd process closed, need to manually open.
crontab achieve timing tasks
linux built-in cron process can help us to achieve these requirements, cron shell script with very complex instructions is not a problem.
cron daemon is a subsystem consisting of small utilities and configuration files, and on almost all UNIX-like systems can be found in a certain style of cron, we can use the ps aux | grep cron daemon crond found.
We often use the crontab command cron table shorthand, it's cron configuration file, or you can call the job list, we can find the configuration file in the following folder.
/ Var / spool / under cron / directory is stored in each user including root's crontab tasks, each with creator named
/ Etc / crontab file is responsible for scheduling the various management and maintenance tasks.
/etc/cron.d/ This directory is used to store any crontab files or scripts to be executed.
We can also put the script in /etc/con.hourly,/etc/con.daily,/etc/con.weekly,/etc/con.monthly directory, make per hour / day / week and month once.
We used commands are:
crontab [-u username] // omit the user table showing the operation of the current user's crontab
-e (edit worksheets)
-l (listed worksheet commands)
-r (delete as work)
We enter the current user's crontab -e worksheet editor, vim is a common interface. Each line is one command.
crontab command configured time + movement, took part in its time, hour, day, month, Friday species, operators have
* All figures within the range of
/ How many numbers every
- From X to Z
Here are a few examples.
002 512 * // in the December 25 0:00
* / 5 * * * * // every 5 minutes
* * * * // 4-6 day 456 Dian
* * * * 2,5 // every Tuesday and Friday
With a simple shell script
If our command logical judgment very complex operation, and then directly edit the crontab bit difficult, then, we can use the shell script. Its origin, definition and classification title match, say no more, we direct it usage.
We used to use vim vim /usr/sh/test.sh edit a shell script
#! / Bin / sh // declares the beginning of shell script a = "hello world" // define a shell variable echo $ a // familiar echo, the output of a variable
Then crontab -e to edit crontab, add * / 5 * * * * /usr/sh/test.sh test.sh once every five minutes to run the script, you can also use / phppath / php /filepath/test.php to use php process to execute php program.