2

I'm running Rasbpian on my Pi, and I have a script in /etc/rc0.d (or more precisely, a symlink to /etc/init.d/myscript.sh) that does some GPIO output to notify the user that power can be shut off.

If I run sudo shutdown -h now, the script runs, and the system goes down.
If I run sudo shutdown now, the script does not run, but the system goes down.

I cannot find any documentation that states what should really happen when running shutdown now with no other arguments.

What difference does the -h make in this case? Does running shutdown now really not invoke runlevel 0?

Update: I found this doc (unixhelp.ed.ac.uk), saying that the default behaviour with no arguments is to go to runlevel 1. But I have a script in /etc/rc1.d, too, that does not run on shutdown.

As requested, here are the first few lines from last -x (the arrows are mine):

pi       pts/3        vidar-ramdals-ma Fri Jan  9 18:07   still logged in   
runlevel (to lvl 2)   3.12.28+         Fri Jan  9 18:04 - 18:07  (00:02)    
reboot   system boot  3.12.28+         Fri Jan  9 18:04 - 18:07  (00:02)    
shutdown system down  3.12.28+         Fri Jan  9 18:04 - 18:04  (00:00)    
runlevel (to lvl 0)   3.12.28+         Fri Jan  9 18:04 - 18:04  (00:00) <- shutdown -h now   
pi       pts/3        vidar-ramdals-ma Fri Jan  9 18:02 - down   (00:01)    
runlevel (to lvl 2)   3.12.28+         Fri Jan  9 18:01 - 18:04  (00:02)    
reboot   system boot  3.12.28+         Fri Jan  9 18:01 - 18:04  (00:02)    
pi       pts/3        vidar-ramdals-ma Fri Jan  9 17:57 - crash  (00:04) <- shutdown now   
pi       pts/3        vidar-ramdals-ma Fri Jan  9 01:30 - 01:34  (00:03)    
pi       pts/3        vidar-ramdals-ma Fri Jan  9 01:29 - 01:29  (00:00)    
runlevel (to lvl 2)   3.12.28+         Fri Jan  9 01:23 - 18:01  (16:38)    
reboot   system boot  3.12.28+         Fri Jan  9 01:23 - 18:04  (16:41)   

The runlevel (to lvl 0) line appears after I did shutdown -h now, while the crash line is from shutdown now. Does shutdown now cause a "crash" which prevents it from running init scripts?

  • Can you run last -x for us? Maybe it's switching to yet a different runlevel. (I have a vague recollection it was runlevel s, single user mode, in slackware?) – Ulrich Schwarz Jan 9 '15 at 11:00
  • Yes, when should I run last -x? After reboot, or before running sudo shutdown now? I'll try when I get home this afternoon. And, BTW, isn't runlevel 1 single user mode? wiki.debian.org/RunLevel – Vidar S. Ramdal Jan 9 '15 at 11:09
  • Anytime, as root. last looks at the history of your logins, reboots and runlevel changes, so it should tell you what runlevel your shutdown puts you in. – Ulrich Schwarz Jan 9 '15 at 12:27
  • OK, I have added that to the question. – Vidar S. Ramdal Jan 9 '15 at 17:10
1

The name of the file in the rc.d folders is important and it's not clear what you've named your symlink to your /etc/init.d/myscript.sh.

Examples from /etc/rc1.d:

  lrwxrwxrwx 1 root root 25 Nov  3  2011 K99readahead_early -> ../init.d/readahead_early
  lrwxrwxrwx 1 root root 25 Nov  3  2011 K99readahead_later -> ../init.d/readahead_later
  lrwxrwxrwx 1 root root 22 Nov  3  2011 S02lvm2-monitor -> ../init.d/lvm2-monitor
  lrwxrwxrwx 1 root root 18 Nov  3  2011 S13cpuspeed -> ../init.d/cpuspeed

If S is supplied then it'll run the script with start; if K is supplied it'll run the script with stop. The integer value is then the priority that should be run.

  • Yeah, should have mentioned, I used update-rc.d to create the symlink, so it is named something like K02.... I'll have a look and find out exactly what the symlink is called. But the problem is still why the script runs on shutdown -h now, and not on shutdown now. – Vidar S. Ramdal Jan 9 '15 at 12:05
  • I thought this issue was due to the different runlevel based on whether -h is provided or not? – jas_raj Jan 9 '15 at 12:24
  • Well, if shutdown now invokes a different runlevel than shutdown -h now - I don't know. I believed that shutdown would cause runlevel 0 no matter what, but maybe I'm wrong. As said, my script runs fine when doing shutdown -h now, but does not run on shutdown now. Another script in /etc/rc1.d also does not run on shutdown now, so it doesn't look like runlevel 1 is invoked. @Ulrich Schwartz suggests that it might be runlevel S that is getting invoked, I'll have to look into that possibility. – Vidar S. Ramdal Jan 9 '15 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.