Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to run your own script automatically when CentOS boots up

Shulou Source: shulou.com Published: 2022-06-01 10:10:22 09月24日 Update

This article will explain in detail how to achieve CentOS boot automatically run their own scripts, Xiaobian feel quite practical, so share with you as a reference, I hope you can gain something after reading this article.

Operating System: CentOS-5.2

Target: boot automatically mount windows shared directory in local area network

The requested URL/etc/rc.d/was not found on this server.

Shell code

su cd /etc/rc.d/ vi rc.local

Second, add the script you want to execute to this file, and the whole content is as follows:

Shell code

#!/ bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local mount //192.168.0.3/data2-1 /mnt/data2-1 -o username=un,password=123 mount //192.168.0.3/data2-2 /mnt/data2-2 -o username=un,password=123 mount //192.168.0.3/data2-3 /mnt/data2-3 -o username=un,password=123 mount //192.168.0.3/data2-4 /mnt/data2-4 -o username=un,password=123 mount //192.168.0.3/data2-5 /mnt/data2-4 -o username=un,password=123

Tip: The practice here is very immature, I hope not, it is best to write a script file to call here, the structure is clearer, but note that the command to be executed is passed to su as a parameter.

Also review a VI editing command-copy

yy

p

3. Although it is done, it is still added

Linux will automatically execute the initialization program in the/etc/rc.d directory when it starts, so we can put the startup task in this directory. There are the following ways:

Option 1:

Simple, that is, the above approach,/etc/rc.d/directory initialization program a lot, rc.local is completed after all the initialization is executed, so it is appropriate to do something here.

Option 2:

The init.d directory is for executable programs, they are actually service scripts, written according to a certain format, Linux will automatically execute at startup, similar to Windows services.

1. Write the following script simpleTest:

Shell code

#!/ bin/bash #chkconfig:2345 80 05 --Specify which levels to execute, 0 generally refers to shutdown, 6 refers to restart, others are normal startup. #description:simple example service RETVAL=0 start(){ #entry function echo "simple example service is started... " } stop(){ #echo "simple example service is stopped... " } #Use case to select case $1 in start ;; stop ;; *) echo "error choice ! please input start or stop";; esac exit $RETVA

3. Run chmod +x /etc/rc.d/init.d/simpleTest to make it executable directly

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

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

Solution 3: The purpose is to mount the shared directory, so you can add it to/etc/fstab.

//192.168.0.3/data2-1 /mnt/data2-1 nfs username=un,password=123 0 0

Sorry, scenario 2, although the script can be run

# ./ simpleTest start

simple example service is started...

But I showed up at step four.

# chkconfig --add simpleTest

Error reading information in simpleTest service: No such file or directory

No further resolution. Sorry.

About "how to achieve CentOS boot automatically run their own scripts" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Tags: Scripts directories services runs scenarios code files programs articles practices entries contents functions commands more levels facets sorry good appropriate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MariaDB NVidia Linux Docker