Solved Execute rc.d script at shutdown

Hello,

I'm trying to install a rc.d script to run at startup and shutdown, but it only run at startup.

Reducing the script to a skeleton test, I have the following:

Code:
#!/bin/sh -
# PROVIDE: test
# REQUIRE: FILESYSTEMS
# KEYWORD: nojail shutdown

. /etc/rc.subr

name="test"
rcvar="test_enable"
stop_cmd="test_stop"
start_cmd="test_start"

test_start()
{
        logger test-start
        touch /var/tmp/test-start
}


test_stop()
{
        logger test-stop
        touch /var/tmp/test-stop
}

load_rc_config $name
run_rc_command "$1"

And in rc.conf:
Code:
test_enable="YES"

Logger only register "test-start" and in /var/tmp there are only one file test-start.


What is wrong?
 
May be I am missing something... But, usually files in /var/tmp are not conserved after reboot.
So, when your script is invoked at shutdown, may be you are not going to have enough time to check the content of that directory.
And after the reboot, every thing is gone...

Also, I think that you should parameterize your script as most as possible:
so, you should replace
Code:
stop_cmd="test_stop"
start_cmd="test_start"

by:
Code:
stop_cmd="${name}_stop"
start_cmd="${name}_start"

and finally, at the last line, I would recommend using "$@" instead of "$1"; because parameters are shifted when using the rc.d framework.
so you should have the following:
Code:
run_rc_command "$@"

Sorry, I can't help more. But you can have a look at this.
Good luck.
 
Thanks for the recomendations but it not change anything.

Sorry, but the files in /var/tmp are not deleted at boot as /tmp.

Thanks,
 
What does service test stop say when run at the CLI?

I've never tried something as described here but reading the comments in /etc/rc.subr it makes it seem there must be a PID file or process running.
Code:
#  stop  if ${pidfile}
#            rc_pid=$(check_pidfile $pidfile $command)
#        else
#            rc_pid=$(check_process $command)
#        kill $sig_stop $rc_pid
#        wait_for_pids $rc_pid
#        ($sig_stop defaults to TERM.)
 
Yes, I read the man page of rc.subr just now and it send kill to the PID.

But the case is for a script that is executed once when system bootup and again when poweroff/restart, a simple script without PID.

How can I approach this scenario?

Thanks,
 
Actually now that I think of it, this is exactly what I do for a small script that maintains some persistent data on a machine that has /var in RAM. I tried the script in your first post and it works as intended for me. What does service test start and service test stop say when run from the command line?
 
The way I read the man page, it has its own function to kill the relevant process. However, if you specify your own stop function, it should run that instead:

For a given method argument, if argument_cmd is not defined, then a default method is provided by run_rc_command:

stop Determine the PIDs of command with check_pidfile or
check_process (as appropriate), kill sig_stop those
PIDs, and run wait_for_pids on those PIDs.
 
Yes, manually it works OK:
Code:
# service test stop
# echo $?
0
# service test start
# echo $?
0
# ls /var/tmp/
test-start    test-stop    vi.recover

But if I clean all /var/tmp/ files and reboot, only:
Code:
# ls /var/tmp/
test-start    vi.recover

And if I reboot again, without delete files:
Code:
# ls /var/tmp/
test-start    vi.recover

The file test-stop isn't there.
 
That was it, as the man page of reboot(8) has:

Code:
Normally, the shutdown(8) utility is used when the system needs to be
halted or restarted, giving users advance warning of their impending doom
and cleanly terminating specific programs.

Because reboot(8) or "halt":
Code:
The halt and reboot utilities flush the file system cache to disk, send
all running processes a SIGTERM (and subsequently a SIGKILL) and, respec-
tively, halt or restart the system.

Thanks!
 
Back
Top