0

I've tried several times with slightly different tutorials (including here) to run a bash script when my Pi boots, however still no luck. I have a simple script that uses screen which I installed so I can run this service, disconnect and check progress via ssh any time. The script, called mine, reads

#!/bin/bash
sleep 5
screen -dmS mine bash
screen -S mine -X stuff "/home/osmc/m-cpuminer-v2-2.4/m-minerd -a m7mhash -o stratum+tcp://xmg.suprnova.cc:7128 -u username.worker-p password -t 4 -e 100
"

I left out my login details here but essentially, I want to use screen to run a CPU miner on boot so if there's any power failure, reboot, what have you, it will automatically start a screen I can SSH into anytime there's a network connection.

I have placed the script in /etc/init.d, I ran update-rc.d mine defaults, I tried update-rc.d mine defaults 100 as per another tutorial and even tried sudo ln -s /etc/init.d/mine /etc/rc6.d all to no avail.

Placing a link to the script in .bashrc does work however, it starts a new session every time I log in when I only want one session to be running in the background.

2 Answers 2

0

The most basic variant is to use /etc/rc.local: this is a shell script that runs during startup. Note that you need to start your script in the background when using rc.local, otherwise it will block execution of other programs started from there until your script terminates. So don't forget the &.

Another way is to write a systemd service unit file, which is a short text file describing your process and how it is to be started and stopped. There are tons of examples available, if you need help, then simply start a new question here and we'll help.

0

Scripts in /etc/init.d are not expected to be just any bash code. You should implement commands like "start", "stop", "restart", etc, and it should terminate rather than running indefinitely. Check out other scripts in there to see how to write one.

2
  • Is it possible to create one which could run indefinitely? Or is there another way to run this script without terminating on boot, possibly with another package I could install?
    – Arran
    Commented Aug 12, 2017 at 10:44
  • Whatever should run indefinitely must be made into a daemon or a background task. The latter is as simple as adding an & at the end of the command. Commented Aug 12, 2017 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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