Run the script program for normal users automatically in Linux.

Source: Internet
Author: User
Article Title: automatically run the common user script program in Linux. 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.

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 datetime
Now = 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 level at which the command is executed. 0 generally refers to shutdown,
6 refers to restart, and others are started normally. 80 indicates the startup priority, and 05 indicates the disabled priority.
# Description: mystart service
RETVAL = 0
Start () {-- start the entry function of the service
Echo-n "mystart serive ..."
Cd/home/test1
Su test1-c "python/home/test1/test. py"

}

Stop () {-- disable the entry function of the service
Echo "mystart service is stoped ..."
}

Case $1 in -- use case to perform interactive operations
Start)
Start
;;
Stop)
Stop
;;
Esac
Exit $ 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.

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.