I have my little script to start an application and do some more things on my Ubuntu 16.04 which looks like this:

./ROC-smi/rocm-smi -d 1 --setfan 90
./ROC-smi/rocm-smi -d 0 --setfan 90
./ROC-smi/rocm-smi -d 2 --setfan 110
./ROC-smi/rocm-smi -d 3 --setfan 110
./ROC-smi/rocm-smi -d 4 --setfan 110
cd teamredminer-v0.4.1-linux
/usr/bin/screen -dm ./teamredminer --algo=cnr --url=*** --user=*** --pass=*** --watchdog_script

I want to autostart it with sudo. I tried rc.local with:

sh /path/to/my/script.sh
exit 0

I also tried using crontab with:

@reboot /path/to/my/script.sh

nothing worked, rc.local didn't show any errors when I tested it with sudo /etc/init.d/rc.local start, but when I type sudo screen -xr or screen -ls or sudo screen -ls, I always got a message that there's no screen to attach.

  • 2
    Why do you want to start this as root? If you want a screen session that your user can use, you need it to be running as your user. – terdon Mar 22 at 15:33

I see there might be two problems with what you're doing.

1. Check your scrip has permission to run

Just chmod-it like

    chmod +x /path/to/my/script.sh
    chmod +x /path/to/ROC-smi/rocm-smi

Otherwise it won't run.

2. Fix wrong paths

When you have ./ROC-smi/rocm-smi you're telling bash to run ROC-smi/rocm-smi from the path you're calling the script, not from where the script is located. This means that if you call the script from /root, bash will look for /root/ROC-smi/rocm-smi and not for /path/to/my/script/ROC-smi/rocm-smi.

The most straightforward solution is

    /absolute/path/to/ROC-smi/rocm-smi -d 1 --setfan 90
    /absolute/path/to/ROC-smi/rocm-smi -d 0 --setfan 90
    /absolute/path/to/ROC-smi/rocm-smi -d 2 --setfan 110
    /absolute/path/to/ROC-smi/rocm-smi -d 3 --setfan 110
    /absolute/path/to/ROC-smi/rocm-smi -d 4 --setfan 110
    cd /absolute/path/to/teamredminer-v0.4.1-linux
    /usr/bin/screen -dm ./teamredminer --algo=cnr --url=*** --user=*** --pass=*** --watchdog_script

However, if that script can run from anywhere, this quick patch will do as pointed out in this answer

    scriptdir="$(dirname "$0")"
    cd $scriptdir"
    ROC-smi/rocm-smi -d 1 --setfan 90

Now, $0 is unreliable (see here) so perhaps you want to try cd "${BASH_SOURCE%/*}" instead. I would try

    cd ${BASH_SOURCE%/*}
    ROC-smi/rocm-smi -d 1 --setfan 90
    ROC-smi/rocm-smi -d 0 --setfan 90
    ROC-smi/rocm-smi -d 2 --setfan 110
    ROC-smi/rocm-smi -d 3 --setfan 110
    ROC-smi/rocm-smi -d 4 --setfan 110
    cd /absolute/path/to/teamredminer-v0.4.1-linux
    /usr/bin/screen -dm ./teamredminer --algo=cnr --url=*** --user=*** --pass=*** --watchdog_script
  • ok, both my scripts were working when im using them throught terminal. I fix all paths to absolute paths like you said, and still it dont work. When i tested it a got that message [ ok ] Starting rc.local (via systemctl): rc.local.service. – mil0sz Mar 22 at 17:12
  • Okay... Well, then it seems to be working pretty much OK once X server is up and running. AFAIK rc.local and cron may run before you have an X user session active, so maybe that's the hint. Can you eg. ./ROC-smi/rocm-smi -d 1 --setfan 90 | echo "1\n" >> ./err.log on each line just to verify the script is being invoked? If that's the case (running but crashing because X isn't ready), try with this answer askubuntu.com/questions/738640/… – Kyordhel Mar 22 at 17:26
  • Hmm, i added that on every line of my script with different numbers and i checked every directory where err.log shoud be created but I found nothing. I checked /etc; /ROC-smi, and teamredminer dir But i saw one thing when i run script manually. I got that error: – mil0sz Mar 22 at 18:45
  • Start with a MWE then. Let script.sh be #!/bin/bash\necho "Chrontab did work $(date)\n" >> /tmp/err.log and then in crontab add @reboot /path/to/script.sh. Reboot and check if works (it should). also check grep cron /var/log/syslog – Kyordhel Mar 22 at 20:22
  • ok, Thank you for your big help, decided to go more straighforward like in answer you sent me, i plug in monitor and manually from gui "Startup Applications" add command to run my script with absolute path and everything is working. – mil0sz Mar 23 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.