In the course of my Linux administrative duties (on a cPanel server), I have created multiple scripts to help us out with Exim, our mail transfer agent. These are mostly used to help us fight spam, and determine who is spamming when it occurs.
This monitors the number of emails in the queue, and sends ours admins an email when a limit (1000) is reached. It would need to be run on a schedule (via cron).
#!/bin/bashexport AdminEmailList="ADMIN EMAIL LIST SEPARATED BY COMMAS HERE"export Num=`/usr/sbin/exim -bpc`if [ $Num-gt 1000 ];thenecho"Too many emails! $Num"| /usr/sbin/sendmail -v "$AdminEmailList"#Here might be a good place to delete emails with “undeliverable” strings within them#Example (See the 3rd script): exim-delete-messages-with 'A message that you sent could not be delivered'fi
This deletes any emails in the queue from or to a specified email address (first parameter). If the address is the recipient, the from must be "<>" (root)
Or, the case of the Android app out-of-order calls
On Android, there is a primary thread which runs all UI stuff. If a GUI operation is ran in a different thread, it just won't work, and may throw an error. If you block this thread with too much processing... well... bad things happen. Due to this design, you have to push all UI operations to this main thread using Looper.run().
Runnables pushed to this thread are always ran in FIFO execution order, which is a useful guarantee for programming.
So I decided to get smart and create the following function to add asynchronous calls that needed to be run on the primary thread. It takes a Runnable and either runs it immediately, if already on the Primary thread, or otherwise adds it to the Primary Thread’s queue.
//Run a function on primary threadpublicstaticvoid RunOnPrimary(RunnableR)
//Start commenting here so that items are always added to the queue, forcing in-order processesingif(Looper.myLooper()==Looper.getMainLooper())
else//End commenting herenewHandler(Looper.getMainLooper()).post(R);
I found it kind of irritating that I had to add actions to the queue when it could have been run immediately on the current thread, but oh well, I didn’t really have a choice if I want to make sure everything is always run in order across the system.
I was recently trying to upload a TV series into Plex and was having a bit of a problem with the file naming. While I will leave the show nameless, let’s just say it has a magic dog.
Each of the files (generally) contained 2 episodes and were named S##-E##-E## (Season #, First Episode #, Second Episode #). Plex really didn’t like this, as for multi-episode files, it only supports the naming convention of first episode number THROUGH a second episode number. As an example S02-E05-E09 is considered episodes 5 through 9 of season 2. So I wrote a quick script to fix up the names of the files to consider each file only 1 episode (the first one), and then create a second symlinked file, pointing to the first episode, but named for the second episode.
So, for the above example, we would get 2 files with the exact same original filenames, except with the primary file having “S02E05,E09” in place of the episode number information, and the linked file having “S02E09-Link” in its place.
The following is the bash code for renaming/fixing a single episode file. It needs to be saved into a script file. This requires perl for regular expression renaming.
#Get the file path info and updated file name
FilePath=`echo"$1"| perl -pe 's/\/[^\/]*$//g'`
FileName=`echo"$1"| perl -pe 's/^.*\///g'`
UpdatedFileName=`echo"$FileName"| perl -pe 's/\b(S\d\d)-(E\d\d)-(E\d\d)\b/$1$2,$3/g'`#If the file is not in the proper format, exit prematurelyif [ "$UpdatedFileName"=="$FileName" ];thenecho"Proper format not found: $FilePath/$FileName"exit 1
fi#Rename the filecd"$FilePath"
mv "$FileName""$UpdatedFileName"#Create a link to the file with the second episode name
NewLinkName=`echo"$FileName"| perl -pe 's/\b(S\d\d)-(E\d\d)-(E\d\d)\b/$1$3-Link/g'`
ln -s "$UpdatedFileName""$NewLinkName"
If you save that to a file named “RenameShow.sh”, you would use this like “find /PATH/ -type f -print0 | xargs -0n 1 ./RenameShowl.sh”. For windows, make sure you use windows symlinks with /H (to make them hard file links, as soft/symbolic link files really just don’t work in Windows).