Run the script program for normal users automatically in Linux.

Source: Internet
Author: User

 

Test environment: Fedora7, Administrator: root, common user: test1

Purpose: When Linux is started, the script program test is automatically run in the root directory of test1. the program automatically appends a record to the local log file each time it is executed. The source code is as follows:

       
        from datetime import datetimenow=datetime.now()f=open(test.log,a)f.write(%s %now)f.close()
       

During Linux Startup, the initialization program under the/etc/rc. d directory will be automatically executed. Therefore, we can put the startup task under this directory in two ways:

Solution 1:

1. Because rc. local is executed after all initialization is completed, we can write the startup script into it.

2. log on to Linux with the root account, vi/etc/rc. d/rc. local, edit the file, and add the following two script programs to be executed:

Cd/home/test1 -- this step is indispensable; otherwise, the system will prompt that you do not have the permission to open the test. log file.

Su test1-c "python/home/test1/test. py" -- use the command to be executed as a parameter to pass the level su

Solution 2:

1. The init. d directory contains executable programs. They are actually service scripts written in a certain format and will be automatically executed during Linux Startup, similar to services in Windows.

2. Log On with the root account and add the following content to vi/etc/rc. d/init. d/mystart:

       
        
#! /Bin/bash # chkconfig: 2345 80 05 -- specifies the execution level. 0 generally refers to shutdown, 6 to restart, and others to start normally. 80 is the startup priority, 05 is the disabled priority machine # description: mystart serviceRETVAL = 0 start () {-- the entry function echo-n "mystart serive... "cd/home/test1su test1-c" python/home/test1/test. py "} stop () {-- close the service entry function echo" mystart service is stoped... "} case $1 in -- use case to perform interactive operations start) start; stop) stop; esacexit $ RETVAL
       

3. Run chmod + r/etc/rc. d/init. d/mystart to execute

4. Run chkconfig -- add mystart to add the service to the configuration.

5. Run chkconfig -- list mystart to view the status of the service process.

Summary:

The core of the two solutions is to switch the user to test1, and then run the command to start the Python program. The advantage of service creation is that multiple interactive commands can be defined, such as start, stop, restart, reset ..., you can also perform operations when the service is running. At the beginning, I wrote the following script according to the general idea, but I couldn't execute it at all:

       
        
Su test1 -- switch to test1 user cd/home/test1 -- switch to the root directory python test. py -- execute the python program exit -- exit the test1 account
       

It seems that everything is correct, but it is found that only the first line of the command is run, and the following is not run until the test1 user exits, it seems that the execution is complete. The reason is that when Linux is started, it is under the root account. Executing su test1 is equivalent to opening a new shell script, therefore, the following code is waiting for the new Shell script to end before it can run. It is like calling a subprogram in the main program, and the subprogram is an endless loop, the following is about to die. Knowing the cause, the solution is relatively simple, that is, do not leave the Shell itself when executing the above script program. We can pass the execution command as a su parameter, because it does not involve opening a new Shell, so we can normally execute the script program you expect, automatically Start the specified script program upon successful startup.

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.