How to run bash script as root on startup?

10,275

The journal is actually telling you why your service won't start.

What it's saying is that service units are not shell scripts. You don't get, apart from some very specialized items, things like ~ expansions or environment variables in service units. The name that you pass to ExecStart must be the ordinary, absolute, path of the program to run. Your name is ~/system/cap.sh, which is (of course) a relative path not an absolute one. This is not the shell. ~ is not a metacharacter. It's an ordinary character, denoting some purported subdirectory of the current directory named ~.

systemd refuses to accept an ExecStart with a relative pathname. And it refuses, obviously enough, to start a service that doesn't have a valid ExecStart that describes how the service is started.

After fixing that, you then need to fix your script so that it's actually a properly executable script. It needs to have execute permission and to have #! spelled properly on the first line. Also note that there's nothing about it that requires the Bourne Again shell, and it's a good idea to follow Debian's lead of using the Almquist rather than the Bourne Again shell in scripts run as part of the system bootstrap.

You'll probably want to tweak your service file so that it doesn't fiddle with the network interfaces in parallel with the system initializing them in the first place. What that means is specific to your system, so you'll have to work out what to put as an After= in your unit file.

Share:
10,275

Related videos on Youtube

Skilo Skilo
Author by

Skilo Skilo

Updated on September 18, 2022

Comments

  • Skilo Skilo
    Skilo Skilo over 1 year

    I am having some problems with this, I have a script that is supposed to start airodump-ng on boot but it usually only runs the first part of the script and thats it.

    I have tried creating a systemd service file and enabling it but the script does not run as root.

    I tried this in both arch linux and debian but in debian i went the init script route but still the script would not run as root.

    My script:

    !#/bin/bash
    ifconfig wlan0 down
    sleep 5
    airmon-ng start wlan0
    sleep 5
    airodump-ng mon0
    
    exit 0
    

    Service file:

    [Unit]
    Description=auto start airmon
    
    [Service]
    ExecStart=~/scripts/cap.sh
    Type=oneshot
    User=root
    
    
    [Install]
    WantedBy=multi-user.target
    

    Edit* This is the output from systemctl status:

    ap.service - auto start airmon
    Loaded: error (Reason: Invalid argument)
    Active: inactive (dead)
    
    Jan 13 13:03:44 alarmpi systemd[1]: [/etc/systemd/system/cap.service:5] Executable   path is not absolute, ignoring: ~/scripts/cap.sh
    Jan 13 13:03:44 alarmpi systemd[1]: cap.service lacks ExecStart setting. Refusing.
    Jan 13 13:47:08 alarmpi systemd[1]: [/etc/systemd/system/cap.service:5] Executable path is not absolute, ignoring: ~/scripts/cap.sh
    Jan 13 13:47:08 alarmpi systemd[1]: cap.service lacks ExecStart setting. Refusing.
    
    • Skilo Skilo
      Skilo Skilo over 10 years
      But when i run the script as root in the terminal everything works fine.
    • JdeBP
      JdeBP over 10 years
      For best results, put the service file and the output of systemctl status $YOURSERVICE (whatever that service is) in your question. And double check that you typed that script in correctly. People are going to immediately jump on one fairly obvious error that might only be your typing error here.
    • Skilo Skilo
      Skilo Skilo over 10 years
      So i changed the location of the script and now it seems to be working, I guess systemd does not like the home directory.
    • Braiam
      Braiam over 10 years
      What are you using, Debian or Arch? Are you using systemd?
    • Skilo Skilo
      Skilo Skilo over 10 years
      Both actually, I have 2 raspberry pi's one with arch and one with raspbian.