# run script before reboot o halt



## ferrillo (Mar 22, 2022)

Good Morning,
I have made a script to log the computer startup and shutdown (shutdown).
I am interested in modifying this script so that when I do a manual reboot or halt it will also register.
In this way I know when it is restarted by a manual reboot, by a power failure, by a shutdown or by a halt.
That is, when I shutdown it works for me, but when I do reboot or halt, it doesn't. how could i do this?
This is my script:

```
#!/bin/sh

# PROVIDE: rmiregistry
# BEFORE: LOGIN
# KEYWORD: shutdown

# Añadir a /etc/rc.conf xxxxx_enable="YES"

. /etc/rc.subr

#rmiregistry_enable=${rmiregistry:-"NO"}

name=rmiregistry
rcvar=`set_rcvar`
pidfile="/var/run/${name}.pid"

start_cmd="start"
stop_cmd="stop"
restart_cmd="restart"
describe_cmd="describe"

load_rc_config $name
start() {
        if [ ! -f $pidfile ] ; then
                echo -e "Starting services: $pidfile"
                echo $$ > $pidfile
                find /usr/local/etc/rc.d/server.log -type f -size 1M -exec rm '>
                date +'Start server: %F %T'>>/usr/local/etc/rc.d/server.log;
                #echo -e "rmiregistry is now running on $pidfile"
                #rmiregistry &
                #echo $! > $pidfile
                #echo "."
        else
                echo "It appears $pidfile is already running. NOT starting!"
        fi
}

stop() {
        if [ ! -f $pidfile ] ; then
                echo -e "It appears $pidfile is not running."
        else
                echo -e "Stopping services: $pidfile"
                date +'Stop server: %F %T'>>/usr/local/etc/rc.d/server.log;
                kill `cat $pidfile`
                rm $pidfile
        fi
}

restart() {
        stop;
        start;

}

describe(){
        echo "Escribe en log el inicio y apagado"
}

run_rc_command "$1"
```



Thanks for the help.


----------



## SirDice (Mar 22, 2022)

ferrillo said:


> That is, when I shutdown it works for me, but when I do reboot or halt, it doesn't.


shutdown(8) runs the rc(8) scripts and gracefully stops all services, halt(8) and reboot(8) do not. 


```
The halt and reboot utilities flush the file system cache to disk, send
     all running processes a SIGTERM (and subsequently a SIGKILL) and,
     respectively, halt or restart the system.  The action is logged,
     including entering a shutdown record into the user accounting database.
```

Systems logs this though, you can find it in last(1) and /var/log/messages.


----------

