Bug 1262663

Summary: The script in /etc/init.d will not be called when the system shutdown.
Product: Red Hat Enterprise Linux 7 Reporter: derrick_liu <derrick_liu>
Component: initscriptsAssignee: initscripts Maintenance Team <initscripts-maint-list>
Status: CLOSED CANTFIX QA Contact: qe-baseos-daemons
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 7.1CC: derrick_liu, pocas.jamie
Target Milestone: rc   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-05-30 12:23:12 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
the log information none

Description derrick_liu 2015-09-14 02:12:58 UTC
Description of problem:
The script in /etc/init.d will not be called when the system shutdown in the RHEL7.1.

But the issue is not present in the RHEL7.0

Version-Release number of selected component (if applicable):
In the RHEL7.1
[root@localhost ~]# chkconfig --version
chkconfig version 1.3.61

In the RHEL7.0

How reproducible:
[root@localhost ~]# chkconfig --version
chkconfig version 1.3.61

Steps to Reproduce:
1.put a init script into /etc/init.d/ directory.
The LSB title is as follows:
===========================================================
#
# chkconfig: 345 99 01
# description: Starts and stops the test daemons used to provide test services.
#
# /etc/init.d/test
#
### BEGIN INIT INFO
# Provides: test
# Required-Start:
# X-UnitedLinux-Should-Start:
# Required-Stop:
# X-UnitedLinux-Should-Stop:
# Default-Start: 3 4 5
# Default-Stop:
# Short-Description: test
# Description: Starts and stops the test daemons used to provide test services.
### END INIT INFO
===========================================================
2. use following command to modify the chkconfig configuration
/sbin/chkconfig --level 345 splx on
3.reboot machine and check the script is called or not when the system shutdown.

Actual results:
The script is not called when the system shutdown.

Expected results:
The script is called when the system shutdown.

Additional info:

Comment 2 Lukáš Nykrýn 2015-09-15 14:00:33 UTC
Was the service running before the shutdown? Systemd will only call stop action on running stuff.

Comment 3 derrick_liu 2015-09-15 14:19:04 UTC
(In reply to Lukáš Nykrýn from comment #2)
> Was the service running before the shutdown? Systemd will only call stop
> action on running stuff.

Yes, The service is running. I use the chkconfig command, do not use the systemd service.  The issue was not present in RHEL7.0, was only present in RHEL7.1

Comment 4 Lukáš Nykrýn 2015-09-15 14:33:17 UTC
Well chkconfig only enables the service. It does not start it.

Can you send me some logs from that machine?
https://wiki.freedesktop.org/www/Software/systemd/Debugging/#index2h1 and the section Shutdown Completes Eventually

Comment 5 derrick_liu 2015-09-16 01:36:49 UTC
1. I use chkconfig command to enable the service, then start the service.

Then I reboot the machine and find that the stop action was not called.

2. i check the document, could you tell me which information are you need.
   Could you tell me the detailed methond to collect the debug log?

(In reply to Lukáš Nykrýn from comment #4)
> Well chkconfig only enables the service. It does not start it.
> 
> Can you send me some logs from that machine?
> https://wiki.freedesktop.org/www/Software/systemd/Debugging/#index2h1 and
> the section Shutdown Completes Eventually

Comment 6 Lukáš Nykrýn 2015-09-16 09:23:08 UTC
Just follow the instructions from the section 
"Shutdown Completes Eventually" from that page

Comment 7 derrick_liu 2015-09-16 11:24:59 UTC
Sorry, Could you tell me how to modify the boot option?

(In reply to Lukáš Nykrýn from comment #6)
> Just follow the instructions from the section 
> "Shutdown Completes Eventually" from that page

Comment 8 Lukáš Nykrýn 2015-09-16 11:32:52 UTC
Put that on kernel cmdline in grub

Comment 9 derrick_liu 2015-09-17 04:38:55 UTC
Created attachment 1074248 [details]
the log information

Comment 10 derrick_liu 2015-09-17 04:41:33 UTC
The splx service did not be stopped when the system shutdown, please check the attachment log file

(In reply to Lukáš Nykrýn from comment #8)
> Put that on kernel cmdline in grub

Comment 11 Jamie Pocas 2015-12-13 07:04:15 UTC
I am running into the same problem, so I recreated a *very* minimal script that reproduces the same problem. Here it is.

--- /etc/init.d/fakeservice ---
$ cat /etc/init.d/fakeservice 
#! /bin/bash

# chkconfig: 345 98 05
# description: fakeservice

lockfile=/var/lock/subsys/fakeservice
logfile=/service.log
timestamp=`date`

case "$1" in
    start)
	echo "${timestamp} service started" >> $logfile
	touch $lockfile
	;;

    force-reload|restart)
	echo "${timestamp} service restarted" >> $logfile
	touch $lockfile
	;;

    status)
	if [ -f $lockfile ]; then
	    echo "${timestamp} service is running" >> $logfile
	else
	    echo "${timestamp} service is stopped" >> $logfile
	fi
	;;

    stop)
	echo "${timestamp} service is stopping" >> $logfile
	rm -f $lockfile
	;;

    *)
	echo "Usage: fakeservice {force-reload,restartstart,status,stop}"
	exit 1
	;;
esac
exit 0 # not always right, but this is just a test
--- EOF ---

I run "chkconfig fakeservice on" to enable it.

Start with "service fakeservice start" as root. I can verify that the lockfile is generated, and the log file is updated.

# reboot

After the machine starts up, I can only see the following.

# cat /service.log
Sun Dec 13 01:42:56 EST 2015 service started
Sun Dec 13 01:43:51 EST 2015 service started

The first line is from when I manually started the service. The second line is from systemd starting starting it on bootup. Notice there is no stop message. If I manually run "service fakeservice stop" and check the log, it shows the stop command working just fine. If I look in /etc/rc.d I see the links to kill the service in runlevel 0 and 6 as expected.

$ find /etc/rc.d/ -name "*fakeservice"
/etc/rc.d/init.d/fakeservice
/etc/rc.d/rc2.d/S98fakeservice
/etc/rc.d/rc3.d/S98fakeservice
/etc/rc.d/rc4.d/S98fakeservice
/etc/rc.d/rc5.d/S98fakeservice

I think it's pretty clear that the kill action for (some?) legacy scripts is not being invoked.

I remember running into a problem in RHEL6/CentOS6 where I needed to put a lockfile with the same name as the init script in /var/lock/subsys, i.e. in this case it's named /var/lock/subsys/fakeservice. I am doing that correctly in this case, at least according to RHEL6 but maybe for some reason in CentOS 7 the expected lock file path was changed, or there is some new additional shutdown prerequisite criteria, then maybe systemd doesn't "think" it has to stop this service.

According to the OP, this issue was not present in 7.0, and was introduced in 7.1. I can confirm it's in 7.1, but I don't have an older 7.0 release machine to test.

Comment 12 Jamie Pocas 2015-12-13 07:07:08 UTC
Please omit the line "$ cat /etc/init.d/fakeservice" at the top of the script section above. If it wasn't obvious, that wasn't intended to be part of the script and it's just how I outputted the contents. My apologies for any confusion.

Comment 13 Jamie Pocas 2015-12-13 16:18:02 UTC
I was able to get this to work. There must have been some subtle script error that wasn't being picked up by 'bash -n'.

Also some other subtle differences, on RHEL6 your shutdown script was invoked by calling the /etc/rc0.d/fakeservice/K05fakeservice script. I used to depend on that in my init script to log a special message that the reason the service was stopping is due to a reboot or shutdown if $0 matched K05fakeservice, as opposed to a normal 'service fakeservice stop' command. However, now it seems to be killed by systemd by running /etc/rc.d/init.d/fakeservice (this is what $0 is set to when your script is run). So it's still possible to tell when someone did 'service foo stop' versus 'reboot'/'poweroff'/etc...

For a 'service foo stop', $0 = /etc/init.d/service
For a poweroff/reboot event, $0 = /etc/rc.d/init.d/cloudarray

This is probably very brittle though, and I think we are being nudged to switch to systemd.

Comment 14 Lukáš Nykrýn 2015-12-14 13:51:14 UTC
In your initscript, do you source /etc/init.d/functions?
Because otherwise you are missing some systemd wrappers.

Comment 15 derrick_liu 2016-03-22 04:34:49 UTC
My script is perl script, so did no source /etc/init.d/functions.

Comment 16 Lukáš Nykrýn 2016-04-05 11:39:10 UTC
Hmm that is the problem, systemd does not know that the service is running, so it will not stop it.

Maybe we should modify service command to run it through systemctl every time for the start action.

Comment 17 Lukáš Nykrýn 2016-05-30 12:23:12 UTC
We can't track services which does not parse the rc.d/init.d/function

Comment 18 derrick_liu 2016-05-31 01:33:21 UTC
Hi Lukáš,


    Could you tell me the detailed logic why the systemd does not know that the servic is running and why team can't track the services.

    Thanks in advancce.

Derrick

Comment 19 Lukáš Nykrýn 2016-05-31 05:39:31 UTC
If you are using shell and source function, than during invocation of initscripts starts this code https://git.fedorahosted.org/cgit/initscripts.git/tree/rc.d/init.d/functions#n593 which call systemctl and this calls the initsript again, but this time systemd can place the process into its cgroup.