liunx开机自动运行命令

1. 通用方法: [root@localhost ~]# vim /etc/rc.d/rc.local

touch /var/lock/subsys/local
# 自动挂载命令
mkdir /xxx
mkdir /var/log/xxx

for ((i=1; i<=5; i++));do
        ping xxx.xx.x.xxx -c 2 | grep -q "ttl=" && echo "CIFS_Server $CIFS_IP is active" >> /var/log/xxx                                                                                      /dhcp.log && break
        dhclient
        sleep 5
        echo "CIFS_Server xxx.xx.x.xxx is not active" >> /var/log/xxx/dhcp.log
done


mount -t cifs -o vers=2.0,username=xxx,password=xxx,sec=ntlmssp,cache=none,nobrl //xxx.xx.x.xxx/xxx /xxx                                                                                        
for ((i=1; i<=3; i++));do
        if [[ "$?" -ne 0 ]];then
                echo "mount /xxx fail" >> /var/log/xxx/dhcp.log
                sleep 5
                mount -t cifs -o vers=2.0,username=xxx,password=xxx,sec=ntlmssp,cache=none,nobrl //xxx.xx.x.xxx/xxx /xxx
        else
                echo "mount /xxx success" >> /var/log/xxx/dhcp.log
                break
        fi
done

设置状态命令:[root@localhost ~]#systemctl status rc-local

2.进阶启动命令设置(注意:运行脚本里需要手动输入指令时)

[root@localhost ~]# vim autotest.sh

#!/bin/bash
rm -rf /var/log/xxx/xxx.log
cd /root/xxx
userNum=""
processID=$(ps -ef | grep xxx.py | grep -v "grep" | awk "{print $2}")
if [ -z "$processID" ];then
        python xxx.py
fi

[root@localhost ~]# vim /etc/profile

autotestfile="/root/xxx/autotest.sh"
# 自动运行本脚本
# autotestfile = '/root/xxx/xxx.py'
if [ -f "$autotestfile" ];then
        sh $autotestfile
        python autotestfile
fi


unset i
unset -f pathmunge

写完脚本后,开始reboot重启进行检查,命令如下

[root@localhost ~]# ps -aux | grep xxx.py

root      3510  0.0  0.1 196356  9732 pts/0   S+   20:51   0:00 python xxx.py
root      3829  0.0  0.0 112812   984 pts/1   S+   21:03   0:00 grep --color=auto xxx.py

简洁方法~忽略上述:


[root@localhost]# vim /etc/profile

autotestfile="/root/xxx.py"
filePath="/root/xxx"
if [ -f "$autotestfile" ];then
        cd $filePath
        python $autotestfile
fi

今日汇总完毕 !!!

你可能感兴趣的:(linux,服务器)