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.
General programming steps for instances
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
This article comes from:Http://boyun.sh.cn/bio? P = 79