Category: Linux

How to stop a bash script from being executed too frequently

Every so often something really useful appears on Reddit, and this is such a case.  You may encounter a situation where you want to execute the contents of a bash script, but not more frequently than every few seconds.  A Reddit user wanted to know How to check if a command in .bashrc has been executed within last 10 seconds if yes don’t execute the command again. The response by Reddit user mdaffin is brilliant in its simplicity, and can be used in any bash script where you don’t want the contents executed too often:

Write a time stamp to some file, check said file before you run the command if now – timestamp > 10s run the command and update the timestamp.

EDIT: Like this (with modification times instead):

TS_FILE="$HOME/.cache/your-app-lock"
if [[ ! -f "$TS_FILE" ]] || [[ "$(expr "$(date +%s)" - "$(stat -c %Y "$TS_FILE")")" -gt 10 ]]; then
touch "$TS_FILE"
echo "running"
fi

You’d replace the echo "running" line with the part of the bash script you want to run only if it’s been 10 seconds since the last time the script was run, or whatever number of seconds you specify after the -gt. If the bash script actually outputs a file as part of its normal operation then you could specify that file in the TS_FILE= line; there would be no need to create a separate timestamp file (unless some other process could also modify that same file).

This doesn’t actually stop the bash script from running; it just prevents it from executing the part of the script that you don’t want executed too frequently. This could be very useful in a situation where without such protection, the too-frequent execution of the script might cause something undesirable to happen (such as getting locked out of an online site for hammering it with requests). Depending on the situation there may be other, perhaps even better ways to avoid this possibility, but in other cases this may indeed be the best approach.

Convert an older model USB printer to a networked printer using a Raspberry Pi or other Linux-based computer — also works well for making an older printer compatible with a newer version of MacOS

We originally set out to do this because we were having problems getting an older model laser printer, specifically a Konica Minolta PP1350W, to work with MacOS High Sierra (10.13). With previous versions of MacOS we’d been able to connect the printer directly to the computer, and with some fiddling with drivers and other software, get it to work. But newer versions of MacOS seem to be far less tolerant of this, and we had a spare Raspberry Pi, so the idea came to us to use the Raspberry Pi as a bridge between the printer and any computers on the local network from which we wanted to be able to print. The bonus is that the printer is no longer tethered to a single machine, but instead can potentially be used by any computer on the local network.

You do not need to have a Raspberry Pi to make this work – any computer that can run Linux will do. And of course the Raspberry Pi or other Linux computer can be used for other purposes besides this. We do not guarantee that this technique will work for every older printer out there, but this will work with a surprising number of them.

Source: Convert an older model USB printer to a networked printer using a Raspberry Pi or other Linux-based computer — also works well for making an older printer compatible with a newer version of MacOS – Two “Sort Of” Tech Guys

PySimpleGUI: Super-simple GUI to grasp… Powerfully customizable (Python)

Looking to take your Python code from the world of command lines and into the convenience of a GUI? Have a Raspberry Pi with a touchscreen that’s going to waste because you don’t have the time to learn a GUI SDK? Look no further, you’ve found your GUI package.

Source: PySimpleGUI (Github)

Note: Scroll down the page for instructions and sample usage. This really does seem like an easy way for Python users to add GUI interfaces to their Python scripts. The author has said in a Reddit post that “It’s based only on tkinter. Zero package dependencies. And it’s a single .py file. That means not much can go wrong.” And that it “Works in Windows, Linux, Mac. Running on my Pi board that has 3.4 installed on it.”

Make LIRC work in Ubuntu 18.04, so that you can use your infrared remote in Kodi

If you are a Kodi user and have recently tried to upgrade your system to Ubuntu 18.04, and then tried to install and use LIRC to make your infrared remote control work the way it should, you may have discovered that it doesn’t work. For one thing, you don’t get the configuration menu during the install process, so you can’t select your make and model of remote. Even the old standby of using sudo dpkg-reconfigure lirc to bring up the configuration menu doesn’t work anymore.

Source: Make LIRC work in Ubuntu 18.04, so that you can use your infrared remote in Kodi – Two “Sort Of” Tech Guys

Send Email from Raspberry Pi Command Line

Send email using SSMTP with the command line on a Raspberry Pi. Step-by-Step Illustrated guide to sending emails through the command line in linux

Source: Send Email from Raspberry Pi Command Line (AlgisSalys.com)

The technique shown in this article, which allows you to send email from the command line or a bash script, uses Gmail to send the mail. It should also work on Debian or Ubuntu based systems.

4 Ways To Keep A Command Running After You Log Out Of The SSH Session

This guide describes how to keep a command running after you log out of the SSH session in Unix-like operating systems.

Source: 4 Ways To Keep A Command Running After You Log Out Of The SSH Session (OSTechNix)

Featured Article:

Recent Posts

Recent Comments

Archives

Categories

Meta

GiottoPress by Enrique Chavez

%d bloggers like this: