• Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Search
  • Register
  • Login
Netgate Discussion Forum
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Search
  • Register
  • Login

How to run a Script before every shutdown?

Scheduled Pinned Locked Moved General pfSense Questions
10 Posts 4 Posters 1.0k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T
    themaharshpatel LAYER 8
    last edited by Jan 7, 2022, 8:44 AM

    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 working

    My script runs a curl command to hit an HTTP request which notifies me that PfSense is shutting down.

    G 1 Reply Last reply Jan 7, 2022, 11:09 AM Reply Quote 0
    • G
      Gertjan @themaharshpatel
      last edited by Jan 7, 2022, 11:09 AM

      @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
      

      No "help me" PM's please. Use the forum, the community will thank you.
      Edit : and where are the logs ??

      T 1 Reply Last reply Jan 7, 2022, 1:49 PM Reply Quote 0
      • T
        themaharshpatel LAYER 8 @Gertjan
        last edited by Jan 7, 2022, 1:49 PM

        @gertjan

        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.

        1 Reply Last reply Reply Quote 0
        • S
          stephenw10 Netgate Administrator
          last edited by Jan 7, 2022, 10:05 PM

          Is it being run at all? If you add some logger output does it show?

          G 1 Reply Last reply Jan 8, 2022, 1:55 AM Reply Quote 0
          • G
            Gertjan @stephenw10
            last edited by Gertjan Jan 8, 2022, 1:56 AM Jan 8, 2022, 1:55 AM

            @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 ?

            No "help me" PM's please. Use the forum, the community will thank you.
            Edit : and where are the logs ??

            T 1 Reply Last reply Jan 8, 2022, 8:30 AM Reply Quote 0
            • T
              themaharshpatel LAYER 8 @Gertjan
              last edited by themaharshpatel Jan 8, 2022, 8:31 AM Jan 8, 2022, 8:30 AM

              @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' anything

              We 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

              B G 2 Replies Last reply Jan 8, 2022, 11:05 AM Reply Quote 0
              • B
                biggsy @themaharshpatel
                last edited by Jan 8, 2022, 11:05 AM

                @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.

                1 Reply Last reply Reply Quote 1
                • S
                  stephenw10 Netgate Administrator
                  last edited by Jan 8, 2022, 1:59 PM

                  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

                  1 Reply Last reply Reply Quote 0
                  • G
                    Gertjan @themaharshpatel
                    last edited by Jan 8, 2022, 4:27 PM

                    @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.

                    No "help me" PM's please. Use the forum, the community will thank you.
                    Edit : and where are the logs ??

                    T 1 Reply Last reply Jan 11, 2022, 5:28 AM Reply Quote 1
                    • T
                      themaharshpatel LAYER 8 @Gertjan
                      last edited by Jan 11, 2022, 5:28 AM

                      @gertjan
                      Thanks! Its Working as expected!

                      1 Reply Last reply Reply Quote 0
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.
                        This community forum collects and processes your personal information.
                        consent.not_received