How to run a shell script at startup

前端 未结 21 1763
伪装坚强ぢ
伪装坚强ぢ 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:13

    In the file you put in /etc/init.d/ you have to set it executable with:

    chmod +x /etc/init.d/start_my_app
    

    Thanks to @meetamit, if this does not run you have to create a symlink to /etc/rc.d/

    ln -s /etc/init.d/start_my_app /etc/rc.d/
    

    Please note that on latest Debian, this will not work as your script have to be LSB compliant (provide, at least, the following actions: start, stop, restart, force-reload, and status): https://wiki.debian.org/LSBInitScripts

    As a note, you should put the absolute path of your script instead of a relative one, it may solves unexpected issues:

    /var/myscripts/start_my_app
    

    And don't forget to add on top of that file:

    #!/bin/sh
    
    0 讨论(0)
  • 2020-11-22 14:13

    Working with Python 3 microservices or shell; using Ubuntu Server 18.04 (Bionic Beaver) or Ubuntu 19.10 (Eoan Ermine) or Ubuntu 18.10 (Cosmic Cuttlefish) I always do like these steps, and it worked always too:

    1. Creating a microservice called p example "brain_microservice1.service" in my case:

      $ nano /lib/systemd/system/brain_microservice1.service
      
    2. Inside this new service that you are in:

      [Unit]
      Description=brain_microservice_1
      After=multi-user.target
      
      [Service]
      Type=simple
      ExecStart=/usr/bin/python3.7 /root/scriptsPython/RUN_SERVICES/microservices    /microservice_1.py -k start -DFOREGROUND
      ExecStop=/usr/bin/python3.7 /root/scriptsPython/RUN_SERVICES/microservices/microservice_1.py -k graceful-stop
      ExecReload=/usr/bin/python3.7 /root/scriptsPython/RUN_SERVICES/microservices/microservice_1.py -k graceful
      PrivateTmp=true
      LimitNOFILE=infinity
      KillMode=mixed
      Restart=on-failure
      RestartSec=5s
      
      [Install]
      WantedBy=multi-user.target
      
    3. Give the permissions:

      $ chmod -X /lib/systemd/system/brain_microservice*
      $ chmod -R 775 /lib/systemd/system/brain_microservice*
      
    4. Give the execution permission then:

      $ systemctl daemon-reload
      
    5. Enable then, this will make then always start on startup

      $ systemctl enable brain_microservice1.service
      
    6. Then you can test it;

      $ sudo reboot now

    7. Finish = SUCCESS!!

    This can be done with the same body script to run shell, react ... database startup script ... any kind os code ... hope this help u...

    ...

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

    Another option is to have an @reboot command in your crontab.

    Not every version of cron supports this, but if your instance is based on the Amazon Linux AMI then it will work.

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

    This is the way I do it on Red Hat Linux systems.

    Put your script in /etc/init.d, owned by root and executable. At the top of the script, you can give a directive for chkconfig. Example, the following script is used to start a Java application as user oracle.

    The name of the script is /etc/init.d/apex

    #!/bin/bash
    # chkconfig: 345 99 10
    # Description: auto start apex listener
    #
    case "$1" in
     'start')
       su - oracle -c "cd /opt/apex ; java -jar apex.war > logs/apex.log 2>logs/apex_error.log &";;
     'stop')
       echo "put something to shutdown or kill the process here";;
    esac
    

    This says that the script must run at levels 3, 4, and 5, and the priority for start/stop is 99 and 10.

    Then, as user root you can use chkconfig to enable or disable the script at startup:

    chkconfig --list apex
    chkconfig --add apex
    

    And you can use service start/stop apex.

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

    Edit the rc.local file using nano or gedit editor and add your scripts in it. File path could be /etc/rc.local or /etc/rc.d/rc.local.

    sudo nano /etc/rc.local
    

    This is the edit:

    #!/bin/sh
    /path-to-your-script/your-scipt-name.sh
    

    once done press ctrl+o to update, pressEnter then ctrl+x.

    Make the file executable.

    sudo chmod 755 /etc/rc.local
    

    Then initiate the rc-local service to run script during boot.

    sudo systemctl start rc-local
    
    0 讨论(0)
  • 2020-11-22 14:16

    Here is a simpler method!

    First: write a shell script and save it a .sh here is an example

    #!/bin/bash
    Icoff='/home/akbar/keyboardONOFF/icon/Dt6hQ.png'
    id=13
    fconfig=".keyboard"
    echo "disabled" > $fconfig
    xinput float $id
    notify-send -i $Icoff "Internal Keyboard disabled";
    

    this script will disable the internal keyboard at startup.

    Second: Open the application " Startup Application Preferences"

    enter image description here

    enter image description here

    Third: click Add. fourth: in the NAME section give a name. fifth: In the command section browse to your .sh . sixth: edit your command section to:

    bash <space> path/to/file/<filename>.sh <space> --start
    

    seventh: click Add. Thats it! Finished!

    Now confirm by rebooting your pc.

    cheers!

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