Scripting help

Status
Not open for further replies.

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
Had an offline question about a simple script I prepared to update my jails. On the off chance the discussion would be helpful to anyone else (and on the greater chance I've made errors that need correcting), I'm answering here.

Sure. Here's what I've done with my script now:
Code:
#!/bin/bash
for JAIL in boinc caddy downloaders plexconnect plexmediaserver urbackup rclone
do
  jexec $JAIL pkg upgrade -y
done

jexec plexmediaserver service plexmediaserver_plexpass restart

for SERVICE in couchpotato sabnzbd sonarr transmission
do
  jexec downloaders service $SERVICE restart
done

The first part is unchanged, of course; the added parts restart the services I want to restart. To break it down a bit further:
  • The first line simply specifies what interpreter will be used to run the script. Bash is one of many shells available (and, of course, many other interpreters--like python and perl--aren't shells at all). It's very popular on Linux, perhaps somewhat less so on FreeBSD. It can be scripted pretty easily, so that someone like me who doesn't know scripting very well can get it working.
  • The next four lines are a for loop. That loop steps through the line(s) that follow for each value specified. The general form is for VALUE in RANGE; do whatever. It's probably most common that RANGE is numeric, but it can also be a set of strings, as it is in this case. In this case, I've specified the names of each of my jails, and for each of them, I'm running jexec $JAIL pkg upgrade -y.
  • The jexec command runs a command inside a jail. It takes two arguments: the name or number of the jail, and the command to run. I'm using jail names, because the numbers can change (if you restart a jail, its number changes).
  • The command I'm running inside each jail is pkg upgrade -y. pkg upgrade upgrades all installed packages, while -y tells it to answer Yes to every confirmation question.
  • After that for loop is complete, the next command is jexec plexmediaserver service plexmediaserver_plexpass restart. From the discussion above, you know that this is running the command service plexmediaserver_plexpass restart inside the plexmediaserver jail. The service command allows you to control various services running on a *nix system. It takes two arguments: the name of the service, and what you want to do to that service. In this case, the name of the service is plexmediaserver_plexpass, and I want to restart it.
  • ...and from the discussion above, it's probably clear what the second for loop does.
 
Last edited:

Jailer

Not strong, but bad
Joined
Sep 12, 2014
Messages
4,977
I wish there were a way to bookmark threads so I could easily find this later.
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
I really like it when someone creates a worth while script like this. While I feel the Resourses tab is filling up with a lot of crap, this would be one of those items worth linking to.

EDIT: How does this work when there is a problem updating a package? Is there some sort of fool-proof way to backup the jail and then if the update fails, then restore the backup? Sometimes the internet may not be avaialble, or the site where the updates comes from, or other issues. I can just see someone complaining about the script messed up all thier jails.
 

Jailer

Not strong, but bad
Joined
Sep 12, 2014
Messages
4,977
Is there some sort of fool-proof way to backup the jail and then if the update fails, then restore the backup?
Snapshots. It's saved me more than a few times when I've screwed up something and had to restore. Just set up a snapshot task to run as often as you feel necessary to circumvent any issues.
 

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
Admittedly, I'm assuming that the package system is reasonably stable, and thus pkg upgrade -y won't leave the system in an unstable condition. But yes, snapshots should address any upgrades gone wrong. I suppose that's even something that could be added to the script if desired.
 

Stux

MVP
Joined
Jun 2, 2016
Messages
4,419
And chmod a+x <script> to make it executable.
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
I'm not paranoid about the script nor automatic upgrades. If I had an issue then I'd resolve it. If some Joe Blow had a problem, they may not know how to fix it. I like the idea of running a snapshot just before the upgrade, but I wouldn't do it for myself.
 

VladTepes

Patron
Joined
May 18, 2016
Messages
287
Well I was the one who asked the original question and that is precisely exactly the type of answer I was looking for. Very clearly explained.

Here, have a dinosaur stamp and a gold star ! :)
 
Status
Not open for further replies.
Top