Back

Linux Startup Script

Create a shell script that runs on startup

This is especially important for setting / resetting the permissions on devices such as GPS receivers

With the updated instructions of adding the web user www-data to the dialout group, this section is now moot for the purposed described above.

inspired by https://transang.me/create-startup-scripts-in-ubuntu/

  1. Create a bash script file. In this example I will use it to reset permissions on a serial port. sudo nano /usr/local/bin/startup.sh
    #!/bin/bash
    FILE=/dev/ttyUSB0
    if [ -e "$FILE" ]; then
        echo "$FILE exists. GPS permissions will be set to 666"
        sudo chmod 666 /dev/ttyUSB0
    else
     echo -e "\e[1;41mWARNING: $FILE does not exist !!\e[1;m"
    fi
    
  2. Set the permissions for this file to be executable: sudo chmod 744 /usr/local/bin/startup.sh.
  3. test the script file: sudo startup.sh
  4. create a .service file: sudo nano /etc/systemd/system/startup.service
  5. add the following lines inside the file:
    [Unit]
    Description=Execute a script on system start
    After=network.target
       
    [Service]
    ExecStart=/usr/local/bin/startup.sh start
       
    [Install]
    WantedBy=multi-user.target
    
  6. start and enable the daemon and check the status:
    sudo systemctl daemon-reload
    # then
    sudo systemctl enable startup.service
    sudo systemctl start startup.service