Scheduled shell script execution in linux

Source: Internet
Author: User
Tags echo command

From: http://blog.csdn.net/jingxiangren/article/details/4745631

In oracle, you can use the dbms_job package to regularly execute pl/SQL and SQL processes. crontab can only be used for scheduled tasks at the operating system level, such as backup.

This article describes the specific usage of crontab for memo.

In oracle, you can use the dbms_job package to regularly execute pl/SQL and SQL processes. crontab can only be used for scheduled tasks at the operating system level, such as backup.

You can use crontab to regularly execute tasks in the following three steps:

1. Write shell scripts

2. Use crontab to join the scheduled task queue

3. View job completion status

I. How to Create shell scripts

There are many different shells in Linux, but we usually use bash (bourne again shell) for programming, because bash is free and easy to use.

The program must start with the following line (must begin with the first line of the file ):

#! /Bin/sh

Symbol #! The parameter used to tell the system that the program is used to execute the file. In this example, we use/bin/sh to execute the program.

When editing a script, you must enable it to execute it.

The following command allows the script to run

Chmod + x filename

Then you can enter./filename to execute the script.

Note: During shell programming, the # symbol indicates the annotation, only until the end of the line. It is best to use annotations when writing programs.

Variable

All variables in shell are represented by strings. variables do not need to be declared and can be directly used. Assign values to variables directly

A = "hello world"

Use the $ symbol to retrieve the variable, for example:

#! /Bin/sh

A = "hello world"

Echo "A is :"

Echo $

The output result is as follows:

A is:

Hello world

Shell commands and Process Control

1) unix Commands

You can use any unix Command in shell, but here we will introduce some more Common unix commands.

Echo ''this is a example ": print the output to the screen

Ls: List objects

Cp sourcefile destinationfile: copy an object

2) concept: pipelines, redirection, and backtick

These are not system commands, but they are often used and important.

Pipeline | use the output of one command as the input of another command

Grep-qa compat | more

Redirection: output the command result to a file instead of a standard output (screen)

> Write the file and overwrite the old file

> The output is appended to the end of the file and the old file is retained.

3) Process Control

If...; then

...

Else if...; then

...

Else

...

Fi

Generally, you can use test commands to test the conditions, such as comparing strings and determining whether a file exists and whether the file has the execution permission.

Generally, "[]" is used to represent the conditional test. Note that spaces are important. Make sure that square brackets have spaces.

[-F "somefile"]: determines whether it is a file.
[-X "/bin/ls"]: determines whether/bin/ls exists and has the executable permission.
[-N "$ var"]: determines whether the $ var variable has a value.
["$ A" = "$ B"]: determines whether $ a and $ B are equal.

InstanceGeneral programming steps

Any excellent script should be helpful and input parameters, and write a pseudo script framework. sh, which contains the framework structure required by most scripts.

Is a very good note. At this time, we only need to execute the cp command when writing the script.

Cp framework. sh myscript

Then insert the function you need.

Debugging

The simplest debugging command is the echo command. You can use echo to print any variable value in any suspected error. This is why most shell Programmers spend 80% of their time debugging programs. The benefit of a Shell program is that it does not need to be re-compiled, and it does not take much time to insert an echo command.

Shell also has a real debugging mode. If an error occurs in the script "strangescript", you can debug it as follows:

sh -x strangescript

This will execute the script and display the values of all variables.

Shell also has a mode that only checks the syntax without executing the script. It can be used as follows:

sh -n your_script

This will return all syntax errors.

Redhat linux AS 3 scheduled task execution

Ii. cron scheduling process

C r o n is the main scheduling process of the system and can run jobs without manual intervention. There is
The c r o n t a B command allows the user to submit, edit, or delete the corresponding job. Each user can have a c r o n t a B file
To save the scheduling information. It can be used to run any s h e l script or a command, once an hour, or a week
Three times, depending on you. Every user can have their own c r o n t a B file, but in a large system,
System Administrators usually disable these files, but only keep one of them in the system. The system administrator
C r o n. d e n y and c r o n. a l o w files to prohibit or allow users to own c r o n t a B files.
3.1.1 crontab Field
To run a job at a specific time, you need to understand the meaning and format of each field in each entry of the c r o n t a B file.
These domains are as follows:
1st columns minute 1 ~ 5 9
2nd columns hour 1 ~ 2 3 (0 indicates midnight)
1-3rd ~ 3 1
4th columns Month 1 ~ 1 2
5th columns in a week 0 ~ 6 (0 indicates Sunday)
6th columns of commands to run
Example format of crontab:
Below is the format of c r o n t a B:
Minute <> hour <> day <> month <> week <> command to run
<> Indicates a space.
An entry in the C r o n t a B file is read from the left, the first column is the minute, And the last column is the command to be run.
After the week.
In these domains, you can use a horizontal bar to indicate a time range. For example, you want to run a task from Monday to Friday.
Business, you can use 1-5 in the week domain. You can also use commas (,) in these domains. For example, if you want
To run a job on Thursday, you only need to use 1, 4. The asterisk (*) can be used to represent consecutive time periods. If you
There is no special limit on a domain that represents time. You should also enter * in this domain *. Each entry of this file must contain 5
Time fields, and each field must be separated by spaces. All the comment lines in this file must be expressed as # At the beginning of the line.
Crontab Entry Example
Here are some examples of the c r o n t a B file entry:
30 21 ***/apps/bin/cleanup. sh
The example above indicates that c l e a n u p. s h in the/a p s/B I n directory of 2 1: 3 0 every night.
45 4, 10, 22 **/apps/bin/backup. sh
The example above indicates the 4: 4 5 running/a p s/B I n directory B a c k u p on the 1, 1, 0, and 2 every month. s h.
10 1 ** 6, 0/bin/find-name "core"-exec rm {};
The preceding example indicates that an f I n d command is run at every Saturday and Sunday.
0, 30 18-23 ***/apps/bin/dbcheck. sh
The preceding example indicates that every day 1 8: 0 0 to 2 3: run between 0 and every 3 0 minutes/a p s/B I n Directory d B c h e c k. s h.
0 23 ** 6/apps/bin/qtrend. sh
The above example indicates that q t r e n d. s h in the 11: 0 0 p m running/a p s/B I n directory on every Saturday.
You may have noticed that in the above example, each command provides an absolute path. When c r o n t a B is used to run s h e l
The user must give the absolute path of the script and set the corresponding environment variables. Remember, since the user is directed to c r o n
If you submit these jobs, you need to provide all the required environments to c r o n. Do not assume that c r o n knows the special environment required.
Actually, I don't know. Therefore, make sure that all necessary paths and environment variables are provided in the s h e l script, except for some automatic
Global variable.
Command Format:
The general form of the c r o n t a B command is:
Crontab [-u user]-e-l-r
Where:
-U user name.
-E: edit the c r o n t a B file.
-L list the content in file c r o n t a B.
-R: Delete the c r o n t a B file.
If you log on with your own name, you do not need to use the-u option, because when you execute the c r o n t a B command, this command can
Know the current user
Create a new crontab file
Before submitting a c r o n t a B file to the c r o n process, you must set the environment variable.
E d I TO R. The c r o n process determines which editor to use to edit the c r o n t a B file. 9 9% u n I x and L I N U X
All users use v I. If so, edit the. p r O f I l E file under the $ H o M e directory.
One row:
EDITOR = vi; export EDITOR
Save and exit.
Create a file named <u s e r> c r o n, where <u s e r> is the user name, for example, j a m e s c r o n. Add the following content to the file.
#.... Here is an explanation
0, 15, 30, 45 18-06 ***/bin/echo 'time'>/dev/console
Save and exit. Make sure that the first five fields are separated by spaces.
In the preceding example, the system outputs the current time to the console every 5 minutes. If the system crashes or hangs,
The last displayed time shows at a glance when the system stops working. In some systems, t y 1 is used for table
Console. You can modify the preceding example accordingly.
To submit the just-created c r o n t a B file, you can use the newly created file as the parameter of the c r o n command:
$ Crontab jamescron
Now the file has been submitted to the c r o n process, which runs every 5 minutes.
At the same time, a copy of the newly created file has been placed in the/v a r/s p o l/c r o n directory, and the file name is the user name (that is,
James ).
List crontab files:
To list c r o n t a B files, you can use:
You will see something similar to the above. You can use this method to perform a backup of the c r O n t a B file in the $ H o M E directory.
Copies:
$ Crontab-l> $ HOME/mycron
In this way, once the c r o n t a B file is accidentally deleted, it can be quickly restored using the method described in the previous section.
Edit the crontab file:
If you want TO add, delete, or edit entries in the c r o n t a B file, and set the environment variable e d I to r to v I
You can use v I to edit the c r o n t a B file. The corresponding command is:
$ Crontab-e
You can modify the c r o n t a B file and exit the file just like you can use v I to edit any other file. If some entries are modified or new entries are added, c r o n performs the necessary integrity check when saving the file. If a field contains a value out of the permitted range, it will prompt you.
Save and exit. It is best to add a comment on each entry in the c r o n t a B file so that you can know
Its functions, running time, and more importantly, know which user's job this is.
You can use the previous crontab-l command to list all its information.
To delete the c r o n t a B file, you can use:
$ Crontab-r

3. You can use the redirection command to output the running result.

>,>>

Iv. Specific instances

Sh script file: test. sh

#! /Bin/sh

Su-oracle <EOF

Sqlplus test/test @ "test. SQL"

SQL script file: test. SQL

Insert into test_tb values (sysdate );

Crontab file:

, 30, 45 */apps/bin/test. sh

 

 

Let's take a note of a problem that we helped solve yesterday:

The subject content is: write a script. The background regularly runs the script at every day and records the information of vmstat 1 5 once.

Analysis: vmstat is used to record the usage of virtual memory. 1 5 means record once every 1 s and record 5 times in total.

Now we need to write a script to complete this function.

Follow the steps described above:

(1) create a file named <u s e r> c r o n, where <u s e r> is the user name, for example, j a m e s c r o n. Add the following content to the file.

00 2 ***/usr/bin/vmstat 1 5>/home/liujiali/cin/liujiali (PS: first, determine the vmstat command path, in this script, we cannot obtain environment variables. The second point is that the data I collected is not stored in the database, but saved to a text file, which simplifies the process)

(2) Submit the file c r o n t a B you just created. You can use this new file as the parameter of the c r o n command:
$ Crontab jamescron

(3) If you want to stop the service, run the following command:

$ Crontab-r

Related Article

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.