Linux起動自己起動スクリプト(Ubuntu:あるユーザで実行、OpenSUSE、CentOS)

2783 ワード

Ubuntu/Debian
rc.localのコマンドは、システム起動後に実行されます。1.編集:
/etc/rc.local
実行したいコマンドを追加すればいいです。
注:特別な場合があります。/etc/rc.localは実行されません。/etc/init.d/rc.local see linkを編集できます。http://www.linuxidc.com/Linux/2016-12/138665.htm
2.所望のユーザでコマンドを実行します。
su -c 'ls -l "myfile.txt"' guowei &
説明:実行が必要なコマンドはls -l "myfile.txt"であり、所望のユーザのguowei&はバックグラウンドで実行される。
3.起動してscreenでプログラムスクリプトを実行し、環境変数(no-login)を追加します。
#!/bin/bash

#        su      ,   sudo -E       
#      root       ,     su -p    su -m         
export GUOWEI_DIR=/home/guowei
export PATH=/opt/bin:$PATH
source /home/guowei/.bashrc

if [ -e /opt/my_app ]; then
#   sudo -E su -p -l guowei << BASH    #      guowei           ,      。 su -          ,sudo      -E   。
    su -p guowei << BASH  #     guowei       ,      sudo,        sudo -E   。
    cd /home/guowei
    screen -d -m /opt/my_app
#	BASH
fi

#     export    -c   ,    -c      :
su guowei -c 'export A=/opt/bla; blabla~; /opt/my_app'  //   :          
その後、ssh登録後、対応するユーザに切り替えられ、screen -lsは、バックグラウンドで実行されるプログラムを見ることができる。
OpenSUSE:
OpenSUSE起動プログラムの設定は特殊です(/ect/rc.localファイルがありません)。主にこの3つのファイル(SysVinit)があります。
1./etc/init.d/after.localこのファイルはデフォルトでは存在しません。手動で作成する必要があります。システムが実行レベルの起動を完了した後に実行するスクリプトです。他のシステムの/etc/rc.localと同様である。
2./etc/init.d/boot.localこのファイルはシステム起動後、実行レベルに入る前に実行するユーザスクリプトです。注意このファイルを実行する時、ネットワークなどの設備はまだ整っていません(ネット接続がありません)。使う時は注意してください。
3./etc/init.d/before.localこのファイルはrcがスクリプトを開始する前に実行されますが、boot.localの後に実行されます。
したがって、ほとんどの起動プログラムには、/etc/init.d/after.localに加えるのが適切である。
システムがSystemdを使用する場合、対応するファイルは:/etc/init.d/after-localTo enable/etc/init.d/after-local:systemctl enable after-local.service To check status of /etc/init.d/after-localsystemctl status after-local.service全体的には、SysVinitシステムを使用するには、一般的なユーザプログラムであれば、ファイルの新規作成のみが必要となる。
vi /etc/init.d/after.local
カスタム起動コマンドを追加すればいいです。
#!/bin/sh

/usr/local/bin/redis-server /etc/redis/6379.conf
ref link:
  • https://unix.stackexchange.com/questions/43230/how-to-run-my-script-after-suse-finished-booting-up
  • https://www.cnblogs.com/toSeek/articles/3011455.html
  • CentOS:
    1、/etc/init.d/ディレクトリの下に新しいファイルを作成します。autostart.sh
    #!/bin/sh
    #chkconfig: 2345 80 80
    #description: auto start my app
    
    sh xxx &  #         
    
    2、cdから/etc/init.dディレクトリに、コマンドを入力します。
    chkconfig --add autostart.sh
    chkconfig autostart.sh on