How to run a Script before every shutdown?
-
Is there any way to run a pre shutdown script which get executed every time PfSense shutdowns?
I tried putting a script in/usr/local/etc/rc.d/shutdown.script.sh
but its not workingMy script runs a curl command to hit an HTTP request which notifies me that PfSense is shutting down.
-
@themaharshpatel said in How to run a Script before every shutdown?:
I tried putting a script in /usr/local/etc/rc.d/shutdown.script.sh but its not working
That's the right place for a script that could be executed when the system starts sending 'STOP' to everybody.
But a lot needs to set up correctly, if you want it to actually work.Like : the file flags are ok ?
Example :[2.5.2-RELEASE][admin@pfsense.here.tld]/root: ls -al /usr/local/etc/rc.d .... -rwxr-xr-x 1 root wheel 120 Jan 5 09:55 shutdown.nut.sh ....
Your "shutdown.script.sh" ha sthe same "-rwxr-xr-x" ?
If there is no "x", the file will not get executed.Another one :
I take my "a file in "" as an example :I found
..... ..... case $1 in start) rc_start ;; stop) rc_stop ;; restart) rc_stop rc_start ;; esac
where rc_strart and rc_stop are local shell script function that do the actual work.
What do you have ?Btw :
When you are connected to the console access (the console, SSH won't do here I guess), and you use menu option 6, you'll see what the system does. Several scripts in /usr/local/etc/rc.d/ will get executed and show some result on the screen.
Use one of these scripts as a template for your own file.You can debug the file by executing yourself :
/usr/local/etc/rc.d/shutdown.script.sh stop
-
Your "shutdown.script.sh" ha sthe same "-rwxr-xr-x" ?
Yes
I checked Script is being executed while I Shutdown my PfSense
My script is a curl command to push me notification on telegram.
Here's my script
#!/bin/bash /usr/local/bin/curl 'https://api.telegram.org/botAPI_TOKEN/sendMessage?chat_id=CHAT_ID&text=URL_ENCODED_TEXT'
I also tried by running
#!/bin/bash curl 'https://api.telegram.org/botAPI_TOKEN/sendMessage?chat_id=CHAT_ID&text=URL_ENCODED_TEXT'
But not getting any notification. If I run this command in console then it pushes the notification.
-
Is it being run at all? If you add some logger output does it show?
-
@hemaharshpatel
A non technical advice : pfSense doesn't shut down itself. Mine can run for months or even years - as long as the admin didn't initiate a shut down. The admin, that's me.
What about a real men to men chat with the people that shut your pfSense down ? And how is it stopped ? Ripping out the power won't 'do' anything ....Btw : maybe a "out of memory" system failure is the exception here : that's up to you to solve that.
The shut down - or reboot (see below) time is logged in the main system log file of course.#!/usr/local/bin//bash /usr/bin/logger About to start the curl ... /usr/local/bin/curl 'https://api.telegram.org/botAPI_TOKEN/sendMessage?chat_id=CHAT_ID&text=URL_ENCODED_TEXT' /usr/bin/logger And done. The error result was $?
Now you have the 'curl' error status code on exit looged; If all went well it should be zero ( 0 ).
Is bash installed ? By default, it isn't.
Is it in /bin/ ? When you install bash, it's here : /usr/local/bin/Read https://forums.freebsd.org/threads/execute-rc-d-script-at-shutdown.53304/
Another thought : sending packets over the cable cost time. Even with a +1Gb/sec connection.
Will the NIC driver even bother if the system wide 'Is about to shut down' is set ? (if such a thing exists) and beginning an action on the wire knowing that the connection might get interrupted ? -
@gertjan
Thanks for the help! Now it works! silly me I though bash would be there. Once I changed to#!/bin/sh
it executed correctly. Now the only problem I am facing is it also gets executed on startup too! So is there flag or parameter check condition I have to keep for only execution at shutdown?A non technical advice : pfSense doesn't shut down itself. Mine can run for months or even years - as long as the admin didn't initiate a shut down. The admin, that's me.
What about a real men to men chat with the people that shut your pfSense down ? And how is it stopped ? Ripping out the power won't 'do' anythingWe usually shut it down at night when there's no use because the hardware is quite old and I think its power supply won't cope up with continuous operation also it is power hungry. Can you give a word of advise regarding this? It would be great! Thanks
-
@themaharshpatel said in How to run a Script before every shutdown?:
I think its power supply won't cope up with continuous operation
Others may or may not agree but my advice would definitely be "leave it on". The power supply and other components in an old machine are far more likely to fail at power-on than they would be while running.
it is power hungry
If power consumption is a major concern, maybe look for a low-power alternative. In fact, if the machine you have is that old, it might be a good idea to have a replacement ready anyway.
-
Yeah, if it's going to fail it will almost certainly be at boot.
There may not be much you cab do about power consumption on an older machine. Unless it's dual CPU and you don't need two CPUs for example.
Both those factors though point to a need to replace it.
Steve
-
@themaharshpatel said in How to run a Script before every shutdown?:
Now the only problem I am facing is it also gets executed on startup too!
Look at the example I gave above :
..... case $1 in start) rc_start ;; stop) rc_stop ;; restart) rc_stop rc_start ;; esac
and also look at other scripts in the /usr/local/etc/rc.d/ folder.
-
@gertjan
Thanks! Its Working as expected!