• 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 Tip: Repeat Commands with Loops and Delays

You are here: Home / Hacks, Tips, and Tutorials / Linux Automation Tip: Repeat Commands with Loops and Delays
FacebookTweetPin
Author: Chris Garrett

Linux and BASH

Unix and Linux are a lot more powerful for automation than people from other operating systems know, that power is under the hood in Macosx (via the BSD Unix foundations) and that power is making its way into Windows in a big way too. Let’s take a look at one simple example …

You might have seen my Python script for creating thumbnails.

What if I wanted to run it continuously?

Well, Linux has an app for that 😉

Create a new file and call it something like “do-thumbs.sh“. Add the following to the file then save:

#!/bin/bash

while [ 1 ];
do
python3 thumb.py
sleep 60s
done

Now enter

chmod +x do-thumbs.sh

This will make your Shell Script executable.

Now if you run the script using

./do-thumbs.sh

It will repeat forever!

Sleep

Note we delay with the sleep command so that we are not pounding the machine.

Sleep can take several time delay options – specify the delay in seconds, minutes, hours or days.

s – seconds
m – minutes
h – hours
d – days

Repeat for a Fixed Count Using For Loops

What if you don’t want it to loop forever?

Just replace the while line with

for x in {1..10}

Related

Category: Hacks, Tips, and TutorialsTag: automation, bash, linux, programming, shell, tips
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:Beautiful prints off the Formlabs Form 2Formlabs Form 2 First Look
Next Post:Email Your Raspberry Pi IP Address on Bootpi-ip

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