Linux self-start and timed Start Program

Source: Internet
Author: User
Article Title: Linux self-start and timed startup programs. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

1. automatically run the program at startup

After Linux is loaded, it initializes the hardware and device drivers, and then runs the first process init. Init starts other processes based on the configuration file. Normally, the modification is placed in/etc/rc or/etc/rc. d or/etc/rc ?. The script file in the d directory can enable init to automatically start other programs. For example, to edit the/etc/rc. d/rc. local file, add "xinit" or "startx" at the end of the file. You can directly enter X-Window after starting the file.

2. automatically run the program upon Logon

When a user logs on, bash automatically runs the global logon script:/ect/profile created by the system administrator. Bash then searches for one of the three special files in sequence in the user's starting Directory:/. bash_profile,/. bash_login, And/. profile, but only executes the first one found.

Therefore, you only need to add commands to the above files as needed to automatically run certain programs (similar to Autoexec. bat in DOS) during user logon ).

3. automatically run the program upon logon.

When you log out, bash automatically runs the personal logout script/. bash_logout. For example /. in bash_logout, add the command tar-cvzf c. source. tgz *. c, the "tar" command is automatically executed every time you log out to back up *. c file.

4. Periodically run the program automatically

Linux has a daemon called crond. Its main function is to periodically check the content of a group of command files in the/var/spool/cron directory, and execute the commands in these files at the specified time. You can use the crontab command to create, modify, and delete these command files.

For example, create a crondFile with the content "00 9 23 Jan * HappyBirthday". After running the "crontab cronFile" command, every time the system automatically executes the "HappyBirthday" program at AM on March 23, lunar January ("*" indicates that no matter the day of the day is the day of the week ).

5. automatically run the program once at a scheduled time

Scheduled command execution at is similar to crond (but it is only executed once): The command is executed at a given time, but is not automatically repeated. The at command is in the format of at [-f file] time. All commands in the file are executed at the specified time. You can also enter the command directly from the keyboard:

$ At 12: 00

At> mailto Roger-s "Have a lunch" <plan.txt

At> Ctr-D

Job 1

At, February 9, an email entitled "Have a lunch.pdf containing the plan.txt file was automatically sent to Roger.

Regular running of programs or scripts is a common problem for administrators,

1. Use crond to monitor program running

1. Use cron to perform tasks on a regular basis

The crond (cron Monitoring Program) can be used to regularly run some tasks, such as backing up logs, database, and sending logs to your mailbox.

Crond is a script that is automatically generated every time Linux is started. The script is/etc/rc. d/init. d. The directory is automatically started every time the system is started.

.

Cron has two configuration files:/etc/crontab, which is a global configuration file. A group of configuration files generated by the crontab command belong to the user level.

Generally, the Administrator only needs to use the global/etc/crontab configuration file. Let's open the configuration file and see:

SHELL =/bin/bash

PATH =/sbin:/bin:/usr/sbin:/usr/bin

MAILTO = root

HOME =/

# Run-parts

01 *** root run-parts/etc/cron. hourly

02 4 *** root run-parts/etc/cron. daily

22 4 ** 0 root run-parts/etc/cron. weekly

42 4 1 ** root run-parts/etc/cron. monthly

Let's analyze this file a bit:

/* Set the shell to run based on. Here we use bash shell */

SHELL =/bin/bash

/* If you specify a command with files in the directory, you do not need the complete directory path */

PATH =/sbin:/bin:/usr/sbin:/usr/bin

/* For details about how to use cron in actual work, refer to the comrade root user via email */

MAILTO = root

/* The main directory related to the/etc/crontab configuration file is the root directory */

HOME =/

/* Well, the key here is: The following is the task under the directory to be specified, and the run-parts command is to run each script under the specified directory */

# Run-parts

/* This statement runs every script file in the/etc/cron. hourly directory one minute after every hour every day */

01 *** root run-parts/etc/cron. hourly

/* Run each script file in the/etc/cron. daily directory at 04:02 every day */

02 4 *** root run-parts/etc/cron. daily

/* Run each script file in the/etc/cron. weekly directory at 04:22 every Sunday */

22 4 ** 0 root run-parts/etc/cron. weekly

/* Run each script file in the/etc/cron. monthly directory at 04:42 on the first day of each month */

42 4 1 ** root run-parts/etc/cron. monthly

You may find it strange to see "*" in it. Let's take a look at the syntax of cron:

The time in the above script is from left to right, and five fields are listed respectively. Let's look at the following table:

------------------------------------------------

Field Value Range

------------------------------------------------

Minute 0 ~ 59

Hour 0 ~ 23, where 0 is midnight and 20 is

Day 1 ~ 31

Month 1 ~ 12

Day of week 0 ~ 7. 0 and 7 indicate Sunday.

------------------------------------------------

Asterisks in any field are wildcard characters. For example, if the first field contains asterisks, a specific task may run at every minute. If you want to specify a time range, such as eight o'clock A.M.

Four o'clock P.M., you can see that the Second Field is set to 8 ~ 16. If you want to run the task every other day, you can set the third field to */2. It can be seen that if the five fields (minute, hour

, Day, month, day of week), each field in cron is no secret.

[1] [2] [3] Next page

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.