Why would a shutdown script NOT run?

2

I have installed a shutdown script on an Ubuntu system which doesn't get executed. It is an Amazon EC2 instance. I'm not sure it has to do with this fact just wanted to point it out.

The script should push some log files to an Amazon S3 bucket so it has to be executed while networking is up.

Here is how I installed the script:

1) Created the file in /etc/init.d/push-apache-logs-to-s3.sh with the required commands.

2) Made it executable with sudo chmod +x push-apache-logs-to-s3.sh

3) Executed sudo update-rc.d push-apache-logs-to-s3.sh start 0 0 .

Output from the above was:

update-rc.d: warning: /etc/init.d/push-apache-logs-to-s3.sh missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
 Adding system startup for /etc/init.d/push-apache-logs-to-s3.sh ...
   /etc/rc0.d/S00push-apache-logs-to-s3.sh -> ../init.d/push-apache-logs-to-s3.sh

The contents of /etc/rc0.d/ is now:

lrwxrwxrwx  1 root root   17 Jul 31  2012 K09apache2 -> ../init.d/apache2
lrwxrwxrwx  1 root root   29 Jun 16  2012 K10unattended-upgrades -> ../init.d/unattended-upgrades
lrwxrwxrwx  1 root root   26 Jun 16  2012 K15landscape-client -> ../init.d/landscape-client
lrwxrwxrwx  1 root root   19 Apr 10 11:11 K20memcached -> ../init.d/memcached
-rw-r--r--  1 root root  353 Jul 26  2012 README
lrwxrwxrwx  1 root root   35 Jul 10 12:01 S00push-apache-logs-to-s3.sh -> ../init.d/push-apache-logs-to-s3.sh
lrwxrwxrwx  1 root root   18 Jun 16  2012 S20sendsigs -> ../init.d/sendsigs
lrwxrwxrwx  1 root root   17 Jun 16  2012 S30urandom -> ../init.d/urandom
lrwxrwxrwx  1 root root   22 Jun 16  2012 S31umountnfs.sh -> ../init.d/umountnfs.sh
lrwxrwxrwx  1 root root   20 Jun 16  2012 S35networking -> ../init.d/networking
lrwxrwxrwx  1 root root   18 Jun 16  2012 S40umountfs -> ../init.d/umountfs
lrwxrwxrwx  1 root root   20 Jun 16  2012 S60umountroot -> ../init.d/umountroot
lrwxrwxrwx  1 root root   14 Jun 16  2012 S90halt -> ../init.d/halt

When I manually execute the script with sudo ./push-apache-logs-to-s3.sh, it does the intended job.

Are these scripts executed by root? What am I missing?

marekful

Posted 2013-07-10T12:35:13.357

Reputation: 163

1This sort of problems are usually caused by something missing from the environment variables when run non-interactively. I assume you took the precaution to not use relative paths on your scripts so maybe a dependence on an environment variable that isn't available. Could you add the script to your question? – GnP – 2013-07-10T13:42:45.533

@gnp I posted another question about this with more details and the script contents. Please check it here: http://superuser.com/questions/617957/lsb-init-script-not-executed

– marekful – 2013-07-10T13:51:50.977

I think I now what's wrong. s3cmd was configured under another user not root thus the init script which is run by root fails to execute the s3cmd command. I'll try configuring it under the root user and see if that helps. – marekful – 2013-07-10T13:58:55.747

Nope, that didn't help. When I execute the script with either user, it is doing what it should so s3cmd CAN access its configuration. Still, apparently the init script is not executed at shutdown... – marekful – 2013-07-10T14:04:34.053

Answers

1

What's the expected behavior when you set the script to be run at S00? I have no scripts with S00 on my system, could it be that a minimum of 1 is required? You also did not explicitly set start/stop, perhaps that's causing problems. Just in case, try

sudo update-rc.d push-apache-logs-to-s3.sh start 01 2 3 4 5 . stop 01 0 1 6 .  

If that still doesn't work, please update your question to who your script (or at least its headers), there might be something wrong there.


EDIT

Having seen the script you posted in your other question, I see two possible problems. First, you have a space in your shebang line !# /bin/sh instead of #!/bin/sh.

More importantly, why are you using sh instead of bash? sh does not support source, let alone . as an alias for source. Change your script to run with bash instead, that should fix it. In fact I don't understand why it works when you run it manually, the . should throw an error:

$ sh
$ source foo
sh: 1: source: not found
$ bash
$ source foo
bash: foo: No such file or directory

Scratch that, I was wrong, as @gnp pointed out, dash does actually support ..

terdon

Posted 2013-07-10T12:35:13.357

Reputation: 45 216

I have tried your suggestion but still no luck. Also, now I modified the script in /etc/init.d/ to be a proper LSB script like all others. Still doesn't get called... – marekful – 2013-07-10T13:25:26.480

@MarcellFülöp and /etc/init.d/push-apache-logs-to-s3.sh is owned by root right? – terdon – 2013-07-10T13:29:31.367

Yes it's -rwxr-xr-x 1 root root – marekful – 2013-07-10T13:34:18.053

On most ubuntu systems sh is a symlink to bash or dash lrwxrwxrwx 1 root root 4 mar 29 2012 /bin/sh -> dash. Also most of the scripts I have on /etc/init.d have the space after the shebang and use sh as the shell. – GnP – 2013-07-10T14:23:15.597

Re: EDIT. Good points. In the first version I had #!/bin/bash. Then I copied one of the existing init scripts and modified it so the #! /bin/sh came from there so I assume that's OK. Will try to change it back to #!/bin/bash though. – marekful – 2013-07-10T14:24:18.707

@gnp yes, but dash does not support source or . so the problem remains since the OP is sourcing a file. The space does indeed seem to be irrelevant. – terdon – 2013-07-10T14:28:16.447

@MarcellFülöp do because . does not run with sh or dash. – terdon – 2013-07-10T14:29:02.527

@terdon dash does support .: `~/workspace$ dash $ echo $a

$ . /home/gabriel/workspace/testfile
$ echo $a
2
$
` – GnP – 2013-07-10T14:40:51.123

@gnp I stand corrected, so it does, I was misreading the error message. – terdon – 2013-07-10T14:44:49.020

1

You want the script to run on shutdown. Replace start with stop as follows:

sudo update-rc.d push-apache-logs-to-s3.sh stop 0 0 .

Note that it will get a K prepended, instead of an S

GnP

Posted 2013-07-10T12:35:13.357

Reputation: 1 266

I've tried that earlier but still no sign of it being executed. – marekful – 2013-07-10T13:25:46.747

That is not what the stop and start do, they have nothing to do with shutdown, that is defined by the run levels given. See man update-rc.d. – terdon – 2013-07-10T13:28:04.673

You're right, it will run when switching to runlevel 0, the stop and start only change the parameter passed to it, which in the OPs case doesn't make any difference. – GnP – 2013-07-10T13:43:54.837

The stop and start are there to specify which runlevels it will start and stop at. stop 0 1 6 means stop at runlevels 0,1 and 6 for example. – terdon – 2013-07-10T14:17:06.267

0

The root of the problem is that s3cmd is not able to read its config file. For some reason unknown to me, during runlevel change (0), when init executes init-scripts, apparently the root user who runs those scripts doesn't count as a "real" user so it doesn't have a "home" directory from where s3cmd tries to read the config.

Explicitly specifying the config file's location using the --config=... solves this problem.

Thanks for the help and input from everyone!

marekful

Posted 2013-07-10T12:35:13.357

Reputation: 163