Category: MacOS

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.

How to Remove Stuck Time Machine Backups from Mac Trash Due to System Integrity Protection Error

If you’re trying to remove a Time Machine backup from a drive and find that it’s stuck in the Mac Trash with a specific error message stating the trash can’t be emptied because “Some items in the Trash cannot be deleted because of System Integrity Protection”, then read on to learn how to resolve this particular Time Machine backup removal problem.

Source: How to Remove Stuck Time Machine Backups from Mac Trash Due to System Integrity Protection Error

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

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)

If you Mac (particularly Mac Mini) goes to sleep or hibernates even though you have disabled sleep in the Energy Saver settings, try this

UPDATED March 19, 2018:

Apple Mac desktop system users (particularly Mac Mini users) that have attempted to disable system sleep or hibernation may have discovered that no matter what changes are made in the Energy Saver pane in System Preferences, when they go away for a while and come back they are greeted by a progress bar (perhaps accompanied by the Apple logo, similar to what would be seen during a reboot) and the only way to recover is to press the power button briefly (which may not always work).

What is weird about this is that in some cases the system for the most part continues to operate normally in the background. So, it is not truly sleeping. If you have music playing, it will continue to play. If someone sends you an instant message, you’ll hear the notification tone. If you click somewhere along the bottom of the screen, if and when the display comes back you will see you have launched some random application from the dock, depending on where you clicked. And so on. But still, that damn Apple logo and progress bar will randomly appear when you wake up the screen.

So, here are some ways to try to fix this, that may or may not work for you. First, open the Terminal application and from the command prompt enter this:

sudo pmset -g

Enter your password when requested and it should show you the System-wide power settings. Note particularly the values for these settings: autopoweroff, hibernatemode, and standby. They SHOULD all have a value of 0 (zero). If any of them have any other value (particularly autopoweroff), then try running this:

sudo pmset -a autopoweroff 0 hibernatemode 0 standby 0

Or you can change any of the values individually, for example:

sudo pmset -a autopoweroff 0

Also, you should increase the standbydelay (the time in seconds before the Mac will try to sleep, and yes it is in seconds no matter what Apple documentation says) to a larger value:

sudo pmset -a standbydelay 86400

Then run sudo pmset -g again and make sure your change was saved. What you are doing is disabling a specific type of sleep mode that has been problematic for many users. If you find that you system still tries to go to sleep, and you have disabled sleep in the Energy Saver settings, then you may need to do one of two things. One is to open a new Terminal window and enter this:

caffeinate

and just let it run until you are ready to shut down your system. Or if that does not work, try disabling display sleep – go to the Energy Saver settings in System Preferences, and move the slider bar for “Turn display off after” all the way to the right, to the “Never” setting. It will warn you that this could shorten the life of your display, but this may be the only thing that actually works. You can always turn off your display using the power switch on the display itself, or if that is in a place that’s difficult to find or hard to press, you could plug the display’s power cord into a switched outlet (such as on a small surge protector strip) and use that switch to turn the display on and off.

Note that you may want to run sudo pmset -g again to make sure that none of the above three settings have changed, if either of the following occur:

1. You make a change to the Energy Saver settings, or

2. You reset the System Management Controller (SMC) on your Mac (note that if your Mac is unplugged or loses power for more than a few seconds, the SMC may be reset).

If any of the three settings mentioned above have changed to a non-zero value, change them back to zero.

I say to TRY this because even doing all this may only reduce the frequency of the sleep states, not eliminate them entirely. The thing that finally made the difference for me was to make the changes to the pmset settings and to disable display sleep completely. There is a real bug in MacOS that needs to be addressed to truly fix this.

For more information on this, search on “autopoweroff” in your favorite search engine.

What Is GREP and How Do You Use It?

Grep is a small Unix program for finding matching patterns. Begun as a Unix program, it can be found on Linux as well as Mac and BSD. It can read just about any text, meaning it can read input from another commands, or it can open and look through files directly. Grep is insanely useful, especially for looking through directories from the command line.

Source: What Is GREP and How Do You Use It? – Make Tech Easier

Recent Posts

Recent Comments

Archives

Categories

Meta

GiottoPress by Enrique Chavez