• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
Maker Hacks

Maker Hacks

Ideas, news & tutorials for makers and hackers – Arduino/Raspberry Pi, 3D printing, robotics, laser cutting, and more

  • Home
  • About
  • YouTube
  • Recommendations
  • Contact

Linux Automation Tips: Running Scripts Automatically

You are here: Home / Hacks, Tips, and Tutorials / Linux Automation Tips: Running Scripts Automatically
FacebookTweetPin
Author: Chris Garrett

Linux Automation Scheduling and Starting Scripts on Boot

Running commands from the shell is all well and good, but what if you don’t want to always be logging into your terminal? What if the script, or even server, crashes or needs to reboot? In this series, we have been looking at automation in Linux, and nothing is more important than automating the launching (and relaunching) of your scripts.

Previously

Linux Automation Tip: Kill Script if Already Running

Sheduling Scripts

The first way we ensure our scripts run is by putting them on a schedule using Crontab. This will launch the commands you set at chosen times, every minute, hour, day, etc.

To edit your schedule, enter:

crontab -e

You can add a command like so:

# m h dom mon dow command
*/30 * * * * /home/chrisg/steemit/reclaim-cron.sh

This launches my Reclaim script every 30 minutes. It checks if I have any Steemit rewards ready to claim so I don’t have to hit the button on the website.

There is an excellent resource for figuring out the schedules here at crontab.guru

Launching on Reboot or Crash

What, though, about starting the scripts on reboot? Or if the script crashes?

You don’t want to run a webserver using Cron, it should be there all the time, right?

There is an app for that! It’s called Supervisor.

Install with:

sudo apt-get install supervisor

When Supervisor runs, it checks for .conf files in /etc/supervisor/conf.d/

Create a new configuration file with a meaningful name:

sudo nano /etc/supervisor/conf.d/[your program].conf

and add your configuration information into the file. In the following, I am creating a test.conf with the program called test.

[program:test]
command=[your command]

[program:test]
user=myuser
autostart=true
autorestart=true
command=/bin/sh /home/chrisg/test.sh
stderr_logfile=/var/log/supervisor/test.err.log
stdout_logfile=/var/log/supervisor/test.out.log

Note I am also outputting two log files, one for script errors and the other for script output. This is important for debugging in case you find something doesn’t run.

You can see that I want my test script to start when the machine boots and if the script crashes for any reason.

When you make changes to your .conf files you will need to re-read and update the configuration, like so:

sudo supervisorctl reread
sudo supervisorctl update

There are also the following commands for stopping, starting and so on.

sudo supervisorctl stop all
sudo supervisorctl reload
sudo supervisorctl restart all

Starting Supervisord on Boot

Of course you also want Supervisor to run on server boot, so you will need to enter the following commands when you are happy with your configuration (Google for your OS and version if this does not work for you):

sudo systemctl enable supervisor
sudo systemctl start supervisor

Tightening Security

Now your machine is doing important work and needs to be semi-reliable, it is a good idea to tighten your security.

First lets stop root logins, you can use Sudo if necessary:

sudo nano /etc/ssh/ssh_config

Find and uncomment, or add the following line:

PermitRootLogin no

If you are only ever running passwordless using Public Key SSH, then you can use the following too:

PasswordAuthentication no

Adding a Simple Firewall in Linux

You can also run a quick and easy uncomplicated firewall called, as it happens, UFW.

Install ufw with:

sudo apt-get install -y ufw

This will stop your machine accepting connections, so now you need to allow the connections you actually want.

I have a dev website running using Flask on port 5000, so I would enter:

sudo ufw allow 5000

Plus obviously HTTP port 80 and, SSH of course:

sudo ufw allow http

sudo ufw allow ssh

Of course if you have an SSL certificate you would add HTTPS.

Check your work with:

sudo ufw status

After making changes, make them take hold using:

sudo ufw --force enable

sudo ufw status


Image Source: Bigstock

Related

Category: Hacks, Tips, and TutorialsTag: linux, making, programming, python, technology
FacebookTweetPin

About Chris Garrett

Marketing Director by day, maker, retro gaming, tabletop war/roleplaying nerd by night. Co-author of the Problogger Book with Darren Rowse. Husband, Dad, 🇨🇦 Canadian.

Check out Retro Game Coders for retro gaming/computing.

☕️ Support Maker Hacks on Ko-Fi and get exclusive content and rewards!

Previous Post:Password-Less SSH Login LinuxPassword-Less SSH Access to Your Linux Machine or Raspberry Pi Using Public Keys
Next Post:Using Grove Sensors, Buttons, and Displays, with Raspberry Pi and PythonGrovePi Gyro, Buttons, Displays

Sidebar

  • Facebook
  • Twitter
  • Instagram
  • YouTube

Recently Popular

  • Gweike Cloud Review
  • How to choose the right 3D printer for you
  • Glowforge Review – Glowforge Laser Engraver Impressions, Plus Glowforge Versus Leading Laser Cutters
  • Original Prusa i3 Mk3S Review
  • Best 3D Printing Facebook Groups
  • Elegoo Mars Review – Review of the Elegoo Mars MSLA Resin 3D Printer
  • Glowforge ‘Pass-Through’ Hack: Tricking the Front Flap of the Glowforge with Magnets to Increase Capacity
  • How to Make a DIY “Internet of Things” Thermometer with ESP8266/Arduino
  • Wanhao Duplicator i3 Review
  • IKEA 3D Printer Enclosure Hack for Wanhao Di3
  • Creality CR-10 3d printer review – Large format, quality output, at a low price!
  • 3D Printed Tardis with Arduino Lights and Sounds
  • Anet A8 Review – Budget ($200 or less!) 3D Printer Kit Review
  • Make your own PEI 3D printer bed and get every print to stick!
  • Upgrading the Wanhao Di3 from Good to Amazing
  • How to Install and Set Up Octopi / Octoprint
  • Creality CR-10 S5 Review

Copyright © 2023 · Maker Hacks · All Rights Reserved · Powered by Mai Theme