Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Job achieve automation in Ubuntu 14.04 - Using Cron     - OpenNMS compile under Linux (Server)

- Matters Oracle 11.2 single instance when connecting ASM need to pay attention and deal with the problem (Database)

- MongoDB study notes - polymerization (Database)

- Java integrated development environment common set of operations (Linux)

- X security settings in Ubuntu (Linux)

- Configuring a Linux operating system against syn attack (Linux)

- Installation salt-minion on RHEL5 (Linux)

- RHEL7.0 environment Linux kernel upgrade (Linux)

- Intrusion analysis and prevention tools Knark under Linux platform (Linux)

- Linux System Getting Started Learning: The Linux logrotate (Linux)

- Deployment Kubernetes manage Docker example cluster on Ubuntu (Server)

- To install Ganglia (Linux)

- C ++ type conversion and RTTI (Programming)

- JavaScript file loader LABjs API Explanation (Programming)

- VMware ghost Linux card error (Linux)

- The Sublime Text 3 configuration file (Linux)

- Using C ++ Container Templates in Shared Memory (Programming)

- Redis logging system (Database)

- For the FreeBSD install Adobe Flash plug-in (Linux)

- PULL operation mechanism parsing XML Comments (Programming)

 
         
  Job achieve automation in Ubuntu 14.04 - Using Cron
     
  Add Date : 2016-09-06      
         
         
         
  Cron is one of the Linux system is the most useful tool, cron job is a job in the specified time comes to be performed scheduling.
The most common automated systems management and automated maintenance work, such as by daily backup schedule to complete the notification, or notification schedule regular clean / tmp / directory. There are many Web applications also need to perform regular job.
This article describes the working mechanism Cron, you can schedule jobs using cron job to achieve. Cron itself is a daemon running in the background, "crontab" to specify the configuration file according to the time schedule jobs to perform.

First, start Cron service

Substantially all of the Linux distributions by default cron tools are pre-installed. Even if not preinstalled cron, is also very simple, run the install it manually:

root @ Ubuntu-14: ~ # apt-get install cron
Then check the cron service status, by default, it should run in the background. If it does not start, you can manually start the service.

root @ ubuntu-14: ~ # service cron start
root @ ubuntu-14: ~ # service cron status
cron start / running, process 1027
Second, the use Cron help

If cron is working properly, then you can use the man command to view the detailed description of the use of its manual.

root @ ubuntu-14: ~ # man crontab
The above command shows how to use the crontab manual describes. To see how to use cron job specific information, it can be:

root @ ubuntu-14: ~ # man 5 crontab

To exit the Help command displays the manual, press the q key or the h key.

Three, Crontab command usage

The following describes how to use the crontab command to achieve timing schedule jobs.

1, Cron Jobs List

Use the following command to list the current user cron jobs plan.

root @ ubuntu-14: ~ # crontab -l
Lists all the current user's cron job, if you want to view other user's cron job, you can use the following command:

root @ ubuntu-14: ~ # crontab -l -u username
This will list the specified user cron jobs.

2, edit Cron jobs

To add a new cron job, or edit an existing cron job, you can use the following command:

root @ ubuntu-14: ~ # crontab -e
3. Remove Cron job

Use the following command to remove cron job has been planned.

root @ ubuntu-14: ~ # crontab -r
Use the following command to remove all scheduled cron jobs, and without confirmation again.

root @ ubuntu-14: ~ # crontab -ir
4, command parameters

-u user: to set a user's crontab services;
file: file is the name of the command file, and said it would file as a crontab task list file and loading crontab. If this file is not specified on the command line, crontab command typed will accept standard input (keyboard) on the command, and they loaded crontab.
-e: crontab edit the contents of a user's files. If you do not specify a user, then edit the current user's crontab file.
-l: Displays the contents of a user's crontab file, if you do not specify a user, then displays the contents of the current user's crontab file.
-r: delete a user's crontab file from the / var / spool / cron directory, if the user is not specified, the default is to delete the current user's crontab file.
-i: When you delete a user's crontab file to the confirmation prompt.

Fourth, the scheduled task Crontab

In addition to the configuration file to handle the program cron job, there are other ways to do it. If you look at the / etc directory, you will find this directory: cron.daily, cron.hourly, cron.monthly like. Therefore, the cron script in these directories, then the system will regularly perform these operations in accordance with these scripts directory name.

1, Cron configuration type

Cron There are two types of configuration files, scheduling automation tasks.

1) system-level Crontab
These cron jobs are used by system services and key job, and requires root-level privileges to perform. You can view the system-level cron job in / etc / crontab file.

2) user-level Crontab
User-level cron job is individual for each user separately. So each user can use the crontab command to create your own cron jobs, you can use the following command to edit or view their own cron jobs.

root @ ubuntu-14: ~ # crontab -e

After selecting the editor, you can configure a new cron job.

Fifth, use Crontab job scheduling

You can use the syntax specified scheduling cron jobs, but also the shorthand abbreviation for commands that allow the management of cron job is simple.
Crontab syntax is as follows:

* * * * * Command to be executed
- - - - - -
| | | | | |
| | | | | --- Pre-order execution
| | | | ----- Represents 0-7 weeks (which can be 0 or Sunday 7 shows)
| | | ------- Indicates the month from 1 to 12
| | --------- Represents the date of 1 to 31
| ----------- Represents hours of 1 to 23 (0 indicates 0:00)
------------- Represents 1 to 59 minutes per minutes * or * / 1 represents
Six new Cron job configuration examples

Now that you are familiar with the crontab command, type grammar and cron jobs, you can now create a number of jobs scheduled for testing. You can use the crontab -e command to add.

1, running every minute scheduled jobs

The following example, create a cron job that text output per minute "test cron job to execute every minute" and send a text to user@vexxhost.com mailbox.
First, use crontab command to edit

root @ ubuntu-14: ~ # crontab -e
Write the following script

SHELL = / bin / bash
HOME = /
MAILTO = "user@vexxhost.com"
#This Is a comment
* * * * * Echo 'test cron job to execute every minute'
: Wq to save and exit!

Once you save this cron script file, you can add it to the program's operations.

2, at a specified time schedule Cron job jobs

If you want to dispatch a cron job, let it "Every Thursday 7:00 pm" Run, then crontab script should be this:

00 19 * * 4 sh /root/test.sh
Then add it to the job scheduler.

root @ ubuntu-14: ~ # crontab -e
crontab: installing new crontab
The above script "0019" refers to the 19 o'clock, "4" refers to Thursday.

Seven summary

We can see, use crontab to automate the task is easy, and it can be minutes, hours, weeks, months, weeks to perform the task. In addition, Linux also at a command, it applies to the processing tasks performed only once, and need to run atd service.
Second, we must pay attention to the issue of environment variables. Sometimes we create a crontab, but this task can not automatically, manually perform this task without any problem, this is usually because there is no configuration environment variable in the crontab file caused. When defining a plurality of scheduling tasks crontab file, you need to pay special attention to set environment variables, because we manually perform a task, in the current shell environment, the program can certainly find the environment variable, and the system automatically performs task scheduling when the load is not any environment variables, and therefore, it is necessary to specify all the tasks required to run the environment variable in the crontab file, so there is no problem when the system scheduled tasks.
Also note that the cleaning system user's mail log. Each task scheduling is finished, the system will output the task information sent via e-mail to the current users of the system, so that over time, the log information will be very large, it may affect the normal operation of the system, so that each task will be redirected treatment is very important.
Finally, note, cron jobs newly created, will not be executed immediately, for at least two minutes before the execution. If you restart the cron service will be executed immediately.
     
         
         
         
  More:      
 
- Vim Getting Started Tutorial (Linux)
- Linux System Getting Started Learning: On Linux how to convert text files to PDF (Linux)
- CentOS How quickly customize kernel binary RPM package (Linux)
- Use Linux built-in firewall to improve network access control (Linux)
- Ubuntu upgrade to Linux Kernel 4.2.3 Kernel (Linux)
- Ubuntu 14.10 install KDE Plasma 5.2 (Linux)
- TypeScript basic grammar (Programming)
- Linux System Getting Started Learning: the local time in between Perl and UNIX timestamp conversion (Linux)
- Installation of Gitlab under Ubuntu (Linux)
- Mac OS X command line to submit the local project to Git (Server)
- HBase vs Oracle (Database)
- Python Flask environment to build (Linux)
- How to understand Python yield keyword (Programming)
- Lua study notes under ubuntu (Linux)
- Oracle Standby Redo Log experiment (Database)
- mysqldump MySQL command-line tool (Database)
- Linux MultiPath software implementation instructions (Linux)
- Kafka cluster deployment (Server)
- DRBD Principle and Features Overview (Server)
- PostgreSQL transaction model introduction (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.