Shutdown script

Status
Not open for further replies.

frenziedengi

Dabbler
Joined
Feb 22, 2013
Messages
14
Hi.

I am looking to run a script (via /bin/sh) at shutdown.
I have a working script, stored on a ZFS volume.
The script requires network connectivity (to send commands to another server via SSH), and the ZFS volume to be mounted (it is normally mounted when the system is running).

I can successfully run the script via command line when I SSH into my server (as root). However, when added to the Init/Shutdown section via the web interface, it does not seem like it is even (attempted) to be executed.

I have added it as:
Type: Script
Script: Full path to script (/mnt/tank/.../myscript.sh)
Type: Shutdown

The first line of the script is:
Code:
#!/bin/sh


I am running 9.2.1.5-RELEASE.

Any tips on how to debug this or provide more information would be appreciated.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Do you plan to tell us any more than the first line? Frankly all you gave us to work with is the proverbial "my car won't start" story. Can't really help you without a boatload more info.
 

frenziedengi

Dabbler
Joined
Feb 22, 2013
Messages
14
What do you need to know? The script is a short script to take over the world.

Let's run with the car analogy. But instead my issue is "the radio antenna won't retract when I turn off the car, but it does when I turn off the radio manually"

So to summarize, the following information was provided
  • I tried turning off the radio manually, and the antenna retracted (I ran the script as root user, which is the user that presumably runs the shutdown script, and it has the intended effect)
  • When I turn off the car with the key, the antenna does not retract (when I run shutdown from the webgui, it does not appear that the script gets run)
  • The model of my car (FreeNAS)
  • The year of my car (9.2.1.5)
  • The type of radio (it's a shell script, to be run by /bin/sh).
If you are worried about the syntax of the script, I would ask what is the difference when running the script manually vs when it runs at shutdown? It seems like as long as the shell (type/version) is the same either way, that shouldn't be an issue.

At this point I am mostly concerned about environment. For example, "shutdown scripts run after network services are brought down" or "shutdown scripts are run after ZFS volumes are unmounted". In either of these two cases I am going to have to do something else since my script is located on a ZFS volume, and I need network access for ping and ssh.
I would rather know: Am I approaching this correctly? It seems like I am following the documentation at: http://doc.freenas.org/index.php/Init/Shutdown_Scripts but it could probably use a bit more detail.

I guess really my question should be: What does the init/shutdown scripts actually tie into in the FreeBSD underpinnings so I can go look into that? A pointer to the directory/file that gets created/modified when you add a init/shutdown script via webgui and a link to some FreeBSD documentation would probably be enough.
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
If you are worried about the syntax of the script, I would ask what is the difference when running the script manually vs when it runs at shutdown? It seems like as long as the shell (type/version) is the same either way, that shouldn't be an issue.

At this point I am mostly concerned about environment. For example, "shutdown scripts run after network services are brought down" or "shutdown scripts are run after ZFS volumes are unmounted". In either of these two cases I am going to have to do something else since my script is located on a ZFS volume, and I need network access for ping and ssh.
I would rather know: Am I approaching this correctly? It seems like I am following the documentation at: http://doc.freenas.org/index.php/Init/Shutdown_Scripts but it could probably use a bit more detail.

I guess really my question should be: What does the init/shutdown scripts actually tie into in the FreeBSD underpinnings so I can go look into that? A pointer to the directory/file that gets created/modified when you add a init/shutdown script via webgui and a link to some FreeBSD documentation would probably be enough.

And that's why I asked for the script. It's very possible the shutdown script runs after some services have shutdown. I don't know, but without the script the creative juices just don't flow far.
 

frenziedengi

Dabbler
Joined
Feb 22, 2013
Messages
14
Doing a bit more research...
I found out that /etc/rc.shutdown is the standard method for shutting down services on FreeBSD.
Examining the /etc/rc.shutdown on my installation I found the following:

Code:
/sbin/ifconfig -l | /usr/bin/xargs -n 1 -J % /sbin/ifconfig % down
sleep 5


When I looked in the FreeBSD source code, these lines were not in rc.shutdown (ref: http://svnweb.freebsd.org/base/release/9.2.0/etc/rc.shutdown?view=markup)

I am curious what the reason would be for bringing down the network interfaces abruptly at the beginning of the shutdown process.

I am guessing that even if my script were run as a script in part of the rc.shutdown process, it wouldn't have a chance of working since the network interfaces would be dead by this point anyway.

That said, I have not been able to locate the "hooks" for where the webgui ties into the init/shutdown process.

And, for completeness, here is the slightly modified script: http://pastebin.com/ZkXJ7p4h
 

frenziedengi

Dabbler
Joined
Feb 22, 2013
Messages
14
Answering all my own questions here:
  • The shutdown scripts to run are stored in the configuration database (makes sense).
  • A query is issued to the database during shutdown for the commands that need to be run by /etc/ix.rc.d/ix-shutdown
  • By commenting out the commands (above) in rc.shutdown I was able to get my script to run (note I needed to modify it in /conf/base/etc of course)
Things seem happy now.
 

solarisguy

Guru
Joined
Apr 4, 2014
Messages
1,125
I understood that you removed the commands that were taking interfaces down.

That could be my mistake, but if not, are you making FreeNAS happy by shutting down the interfaces at the end of your script?

Or you added another script, to be run after yours, to shut down the interfaces?
 
Status
Not open for further replies.
Top