Yoan
Yoan

Reputation: 95

Most reliable way to run a script indefinitely on Linux

I've got a few linux devices that I have to set up. Once set up, they'll be at a remote location and I won't be able to get them back. I'm using raspberry pi. These devices will run several scripts, that have to work indefinitely for month or years. The scripts that are running on the devices:

What I've done so far:

Cron job

When the device boots, a bash script is launched:

@reboot sudo bash /home/user/Python/Python_bash_1.sh

There is the bash scripts that checks if the Python script is running:

#!/bin/bash
until python3 /home/user/Python/program_1.py; do
    echo "'program_1.py' exit code $?.  Program restarting.." >&2
    sleep 1
done

Rc.local

The other option is to use rc.local:

#SSH auto connection
autossh -M 10001 -N -f -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=no" -i /root/.ssh/rsa_rasp_dev_1 -R 6001:localhost:22 root@middle_ip_address -p 22 &
exit 0

With the -f option and the ampersand (&) that works well.

After one week of testing and a few reboots, it looks reliable, but I've no idea if it's going to last months or years.

My questions are:

What is the best way between crontab and rc.local to launch a bash ? Are there other ways to check if a script is running and relaunch if it's not ?

Thanks,

Upvotes: 1

Views: 754

Answers (1)

JimiDini
JimiDini

Reputation: 2069

There are special tools that take care of that.

  1. Modern init systems such as upstart or systemd has this bundled: you just define that service should be restarted in case of failure
  2. On older systems you can use custom piece of software such as daemontools or supervisord

Upvotes: 1

Related Questions