How to start and run programs automatically under Linux __linux

Source: Internet
Author: User

1. Automatically run the program at power-on startup

After Linux is loaded, it initializes hardware and device drivers, and then runs the first process init. Init continues the boot process based on the configuration file to start another process. Typically, modifications are placed in the

/ETC/RC or

/ETC/RC.D or

/etc/rc? D

A script file in the directory that enables Init to start other programs automatically. For example: Edit the/etc/rc.d/rc.local file (which is usually the last script to be launched by the system), at the end of the file with a line of "Xinit" or "startx", you can enter the X-window directly after the boot.

2. Automatically run the program at logon

When a user logs on, bash automatically executes the global logon script created by the system administrator:

/ect/profile

Bash then finds one of the three special files sequentially in the user's starting directory:

/.bash_profile,

/.bash_login,

/.profile,

But only the one that was first found is executed. Therefore, simply adding a command to the above file in the light of the actual need enables the user to automatically run certain programs (similar to the DOS Autoexec.bat) when they log in.

3. Automatically run the program when you exit the login

Bash automatically performs a personal exit logon script when exiting the login

/.bash_logout.

For example, by adding the command "Tar-cvzf c.source.tgz *.c" in/.bash_logout, the "tar" command is automatically backed up *.c files each time you exit the login.

4. Regular automatic running of the program

Linux has a daemon called crond, which periodically checks the contents of a set of command files in the/var/spool/cron directory and executes the commands in those files at set times. Users can create, modify, and delete these command files through the crontab command.

For example, the establishment of a document Crondfile, content of "9 hours * happybirthday", running the "crontabcronfile" command, every 23rd 9:00 system automatically executes the "HappyBirthday" program ("*" It means no matter what day of the week it is.

5. Timed automatic Running program once

The timed execution command at is similar to Crond (but it only executes once): The command executes at a given time, but does not automatically repeat. The AT command is in the general format: at [-F file] time, executing all the commands given in the file at the specified times. You can also enter commands directly from the keyboard:

$ at 12:00

At>mailto Roger-s″have a lunch″< plan.txt

At>ctr-d

Job 1 at 2000-11-09 12:00

2000-11-09 12:00 automatically issued a title "Have a Lunch", content for Plan.txt file content of the mail to Roger.

Add the boot script under Ubuntu

Ubuntu will execute the script in the/etc/rc.local file after boot, so we can add the startup script directly to/etc/rc.local.

Of course to add to the statement: Exit 0 before the line.

Such as:

sudo vi/etc/rc.local

Then, add the scripting code before exit 0.

Follow up with additional research to add an Ubuntu boot service to see.

If you want to add a script file to boot execution, you can either copy or soft connect the script to the/etc/init.d/directory, and then add the script to the queue where the execution is initialized by: update-rc.d xxx defaults nn command (NN is the boot order).

Note If the script requires a network, then NN needs to set a larger number, such as 98.

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.