I'm learning to use shell script on linux (I'm working on CentOS 6.7) and file system too. Now I'm creating a script that writes messages in a text file when the system is started up or shut down. I've placed the following in /etc/init.d:
#!/bin/sh
#
# This is a test that send messages to a log
LOGPATH=/home/user/documents
now=$(date +'%T')
start() {
echo "[$now] System startup" >> $LOGPATH/test.log
}
stop() {
echo "[$now] System shutdown" >> $LOGPATH/test.log
}
status() {
echo "[$now] Hi, you're checking status" >> $LOGPATH/test.log
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
$0 stop
$0 start
;;
status)
status
;;
*)
## If no parameters are given, print which are avaiable.
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac
Then I created K symblinks in rc0 and rc6 for shutdown and reboot and S symblinks in rc5 (my default).
It worked on startup, but it didn't on shutdown. After rebooting a few times trying different things, I only have "[time] System startup", even giving a K01 priority to the script in rc0.
Why is not working on shutdown? Maybe Kill signal on reboot doesn't work like I think it works, but I'm just not sure.
What you are trying to do is writing a service
. As such you should not create those symlinks manually, instead you should use the chkconfig
command to add your shell script as a new service.
There are special comment lines that you need to add at the top of your script for chkconfig
to handle it correctly. Take a look at both the man pages for chkconfig
and service
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.