How to run a shell script at startup

前端 未结 21 1761
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 13:39

On an Amazon S3 Linux instance, I have two scripts called start_my_app and stop_my_app which start and stop forever (which in turn runs my

相关标签:
21条回答
  • 2020-11-22 14:06

    For some people, this will work:

    You could simply add the following command into SystemPreferencesStartup Applications:

    bash /full/path/to/your/script.sh
    
    0 讨论(0)
  • 2020-11-22 14:08

    Just have a line added to your crontab..

    Make sure the file is executable:

    chmod +x /path_to_you_file/your_file
    

    To edit crontab file:

    crontab -e
    

    Line you have to add:

    @reboot  /path_to_you_file/your_file
    

    That simple!

    0 讨论(0)
  • 2020-11-22 14:09

    For Debian 9 see https://askubuntu.com/questions/228304/how-do-i-run-a-script-at-start-up. It is helped me. Short version for Debian 9: add commands (as root) to /etc/rc.local

    /path_to_file/filename.sh ||  exit 1   # Added by me
    exit 0
    

    Probably, /path_to_file/filename.sh should be executable (I think so).

    0 讨论(0)
  • 2020-11-22 14:11

    Set a crontab for this

    #crontab -e
    @reboot  /home/user/test.sh
    

    after every startup it will run the test script.

    0 讨论(0)
  • 2020-11-22 14:11

    This simple solution worked for me on an Amazon Linux instance running CentOS. Edit your /etc/rc.d/rc.local file and put the command there. It is mentioned in this file that it will be executed after all other init scripts. So be careful in that regards. This is how the file looks for me currently.. Last line is the name of my script.

    0 讨论(0)
  • 2020-11-22 14:12

    Enter cron using sudo:

    sudo crontab -e

    Add a command to run upon start up, in this case a script:

    @reboot sh /home/user/test.sh

    Save:

    Press ESC then :x to save and exit, or hit ESC then ZZ (that's shift+zz)

    Test Test Test:

    1. Run your test script without cron to make sure it actually works.

    2. Make sure you saved your command in cron, use sudo crontab -e

    3. Reboot the server to confirm it all works sudo @reboot

    0 讨论(0)
提交回复
热议问题