[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Linux startup, Wheezy -- a required script won't run on startup, but can run manually without any trouble



Hi,

I'm not sure my end solution is the best.

TLDR; -- I've now got the script working as desired, but I have more to
say below, including a thank you for Christian in particular.  And I do
have other queries.


I want the script to run once only at bootup, before exim4 and also
before dovecot, it isn't a service; but I've moulded the script to
appear like one in order to achieve the desired result.  And there is no
need to run it ever again after startup and there is nothing to do at
shutdown.

I couldn't use /etc/rc.local as that would act after everything was up
and running normally.  Probably I really should have the script
somewhere else, but I'm not sure where exactly would be best.  Hence why
it ended up in the initscript area.  Perhaps people have some other
suggestions?  I would be happy to hear them here.

On 10/06/2016 7:29 AM, Christian Seiler wrote:
> On 06/09/2016 07:46 PM, Andrew McGlashan wrote:
>> The order of the scripts alone allowed for everything to be very, very
>> simple and no script relied upon any other; they were self dependent.
>> If you wanted something to be available before your script, you made
>> sure your numeric number after the S in the script name (or rather the
>> symlink name back to the /etc/init.d directory file) was higher.  It was
>> simple, it worked perfectly,
> 
> (In the following, for the most part I'm only going to talk about
> sysvinit, ignoring any other init system.)
> 
> I think you are suffering from quite a bit of confusion. You need to
> separate a few concepts apart:

Yes.

>  - the S**/K** symlinks
>  - how they are generated
>  - startup parallelization
> 
> Since very old versions of Debian (I don't remember which), you could
> create symbolic links for init scripts like this:
> 
>  - /etc/rcX.d/SYYname -> /etc/init.d/name
>  - /etc/rcX.d/KYYname -> /etc/init.d/name
> 
>    YY being a number between 00 and 99 here.

Yes, but if you did a simple: "ls -l S*" or "ls -l K*", then the order
was properly determined.

> The main problem with this scheme alone is that the numbers are
> actually really arbitrary, so it's not immediately clear which ones to
> use when writing an init script.

Not arbitrary... I numbered them according to the desired sequence,
knowing myself which processes needed to be started before others.  So,
by design of the chosen numbers and script names, I was previously able
to run scripts in the order that I knew was required by my own resolve
and dependencies were not complex enough to require /special/ processing
outside my own resolve.

> To enable a service initially, you'd call
> 
>   update-rc.d defaults NAME

After script adjustments....

# update-rc.d archive-system-mail remove
update-rc.d: using dependency based boot sequencing


# update-rc.d archive-system-mail defaults
update-rc.d: using dependency based boot sequencing

>> Now I have sysvinit isntalled on wheezy, it is failing to run a simple
>> script during system boot (as part of a planed reboot) and I cannot work
>> out why.
>>
>> # ls -l /etc/{init.d,rc*.d}/*archive*
>> -rwxr-xr-x 1 root root 1453 Jun  8 04:12 /etc/init.d/archive-system-mail
>> lrwxrwxrwx 1 root root   29 Jun  3 23:30
>> /etc/rc2.d/S02archive-system-mail -> ../init.d/archive-system-mail
>> lrwxrwxrwx 1 root root   29 Jun  4 03:06
>> /etc/rc3.d/S02archive-system-mail -> ../init.d/archive-system-mail
>> lrwxrwxrwx 1 root root   29 Jun  4 03:07
>> /etc/rc4.d/S02archive-system-mail -> ../init.d/archive-system-mail
>> lrwxrwxrwx 1 root root   29 Jun  4 03:08
>> /etc/rc5.d/S02archive-system-mail -> ../init.d/archive-system-mail

I still have low numbers, but done the correct way via insserv.

# ls -l /etc/{init.d,rc*.d}/*archive*
-rwxr-xr-x 1 root root 2402 Jun 10 15:19 /etc/init.d/archive-system-mail
lrwxrwxrwx 1 root root   29 Jun 10 15:38
/etc/rc0.d/K02archive-system-mail -> ../init.d/archive-system-mail
lrwxrwxrwx 1 root root   29 Jun 10 15:38
/etc/rc1.d/K02archive-system-mail -> ../init.d/archive-system-mail
lrwxrwxrwx 1 root root   29 Jun 10 15:38
/etc/rc2.d/S02archive-system-mail -> ../init.d/archive-system-mail
lrwxrwxrwx 1 root root   29 Jun 10 15:38
/etc/rc3.d/S02archive-system-mail -> ../init.d/archive-system-mail
lrwxrwxrwx 1 root root   29 Jun 10 15:38
/etc/rc4.d/S02archive-system-mail -> ../init.d/archive-system-mail
lrwxrwxrwx 1 root root   29 Jun 10 15:38
/etc/rc5.d/S02archive-system-mail -> ../init.d/archive-system-mail
lrwxrwxrwx 1 root root   29 Jun 10 15:38
/etc/rc6.d/K02archive-system-mail -> ../init.d/archive-system-mail

>> Now, I want the archiving script to run on system startup, I don't want
>> dovecot or exim4 to be running when the script starts, it simply needs
>> to have the /backup and /var file systems mounted to do it's required job.
> 
> Ok, so if you depend on local or remote file systems, you need to
> depend on $local_fs and $remote_fs, respectively. If all your file
> systems are local, use $local_fs - otherwise $remote_fs (or both).
> 
> (Technically, $syslog might suffice, because all standard syslog
> implementations provide $syslog via /etc/insserv.conf, and they
> themselves depend on $local_fs at least, but IMHO it's better to
> explicitly write out $remote_fs and $local_fs.)
> 
>> My script is meant to create a log file in the /var/log directory.  If I
>> run the script manually, it works perfectly.  There are some generic
>> parts in the script, it is a fairly simple script, even if a little bit
>> over complicated.  What do I need to do to fix it?
>>
>> Here is the script:

Here is the script now, it seems to work correctly now:

# cat archive-system-mail
#! /bin/sh
### BEGIN INIT INFO
# Provides:          mailarchive
# Required-Start:    $local_fs $remote_fs $syslog
# Required-Stop:     $local_fs $remote_fs $syslog
# X-Start-Before:    exim4 dovecot
# X-Stop-After:      exim4 dovecot
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Save exim4 system_filter archive
# Description:       This file should be ran at boot time before either
#                    exim4 or dovecot is started.  It saves mail that is
#                    created via rules within the /etc/system_filter and
#                    it's related log file.
### END INIT INFO

# Author: Andrew McGlashan <andrew.mcglashan@affinityvision.com.au>

set -x

DESC="Save exim4 system_filter archive"
NAME=archive-system-mail
SCRIPTNAME=/etc/init.d/$NAME

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

do_cleanup()
{
        # Cleanup
        echo -e "\a\a\aPerforming cleanup"
        exit $1
}

do_error_exit()
{
        # Display error message and exit
        echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
        do_cleanup 1
}

do_get_archivedir()
{
        for VER in a b c d e f g h i j k l m n o p q r s t u v w x y z
        do

ARCHIVE_DIR=/backup/mail/system_filter_all_mail/archive/${DATEX}${VER}
                if [ ! -d $ARCHIVE_DIR ]; then return 0; fi
        done
        return 1
}

do_main()
{
        (
        do_get_archivedir || do_error_exit "Cannot get archive dir"

        if [ ! -d ${ARCHIVE_DIR} ];then /bin/mkdir $ARCHIVE_DIR;fi
        /bin/mv /backup/mail/system_filter_all_mail/log--lots $ARCHIVE_DIR/
        /bin/mv /backup/mail/system_filter_all_mail/Maildir   $ARCHIVE_DIR/
        /usr/bin/touch $ARCHIVE_DIR/log--lots.$$
        )  2>&1 | /usr/bin/tee /var/log/${PROGNAME}--$(date
+%Y%m%d%H%M%S).log
}

# main
trap "do_cleanup 99" HUP INT TERM

# Global Variables
ARCHIVE_DIR=
VER=
DATEX=$(/bin/date +%Y%m%d)
PROGNAME=$(/usr/bin/basename $0)

case "$1" in
        start)
                [ "$VERBOSE" != no ] && log_daemon_msg "Running Startup
Script $DESC" "$NAME"
                do_main
                [ "$VERBOSE" != no ] && log_daemon_msg "Archive
Directory for $NAME" "$ARCHIVE_DIR" "$VER"
                ;;
        stop|status|restart|force-reload) exit 0 ;;
        *)
                echo "Usage: $SCRIPTNAME {start}" >&2
                exit 3
                ;;
esac

:



Thank you all, especially Christian for the excellent explanations and
helping me learn the errors of my ways.  And, like I said above, happy
to learn some more even though the script is now running as desired.

Kind Regards
AndrewM

Attachment: signature.asc
Description: OpenPGP digital signature


Reply to: