Run script at the end of run level 3.

Hello I would like to run a script after I am sure that the network interfaces are up. I was thinking that this might be the end of init 3 or init 5.

In which file should I execute my script?

Best Regards
Alex

I am not an expert on what scripts are allowed while in runlevel 3, but you could add a symlink to your script to be run while in rl3 like this in a terminal session:

sudo ln -s /path/to/script/the_script /etc/rc.d/rc3.d/the_script

You do not copy any scripts to here. A good location for the script might be /usr/local/bin for instance. You must have root privilege to perform this function. You can test your script to make sure it works by going to rl3 in this manner:

su -
password:
init 3
root
password:
script_name

Thank You,

When you create a file* /etc/init.d/after.local*, the commands in this file will be run at the end of any runlevel changing. Thus when you want to run your-script on reaching runlevel 5:

test "$RUNLEVEL" = "5" && /path/to/your-script

must be there. I leave it to you to let it run on also on reaching runlevel 3. And of course, you may like another contruct then the old fashioned test :wink:

Thank you very much I also tried your approach but are u sure your correct is correct?

it seems that in my system there is no $RUNLEVEL variable and that is my script does nothing as the test $RUNLEVEL always fail.

Best Regards
Alex

I am rather sure it is. Please do;

grep RUNLEVEL /etc/init.d/*

to show yourself that it is used extentive in the scripts.

I also used this in another case and it worked thare.

But when you want me to look at what you did (after all you there could be something wrong), you better post what you have there:

ls -l /etc/init.d/after.local
cat /etc/init.d/after.local

I put some start ** to hide some characters

cs:/home/ # ls -l /etc/init.d/after.local
-rwxr-xr-x 1 root root 113 2011-01-31 14:09 /etc/init.d/after.local
cs:/home/aa # cat /etc/init.d/after.local
#! /bin/bash
test “$RUNLEVEL” = “3” && /home/a
***/mountmy****orn.sh

  1. please put computer code between CODE tags (as I did for you): Posting in Code Tags - A Guide.

  2. it is up to you to make a lot of things obscure here, personaly I would only do that with passwords and thngs like e-mail addresses. Let us hope that our debugging is not hampered by reading *s everywhere.

  3. you have only a test on runlevel 3 here. I hope you do boot in runlevel 3 to test this.

  4. this also assums that /home/a****/mountmy****orn.sh is an executable file, did you check?

  5. I suppose you are aware that this will run as root. That means that a script in a place that apparently is owned by a normal user will run as root. That is not a good idea. That user can put everything there that can completely destroy your system.

Hello again,
I did some improvements. I hope it is better now

ls -l /etc/init.d/after.local 
-rwxr-xr-x 1 root root 113 2011-01-31 14:09 /etc/init.d/after.local

cat /etc/init.d/after.local 
#!/bin/bash
test "$RUNLEVEL" = "5" && /root/mountmypart.sh

 cat /root/mountmypart.sh 
#! /bin/bash
mount /home/myuser/Documents/Documents-Mounted 


 grep RUNLEVEL /etc/init.d/*
/etc/init.d/after.local:test "$RUNLEVEL" = "5" && /root/mountmypart.sh
/etc/init.d/alsasound:  if  "$RUNLEVEL" = "6" -o "$RUNLEVEL" = "0" ]; then
/etc/init.d/boot.cleanup:       if test "$1" = "start" -o "$RUNLEVEL" = "6" ; then
/etc/init.d/kbd:                case "$RUNLEVEL" in
/etc/init.d/kbd:        case "$RUNLEVEL" in
/etc/init.d/kbd:        case "$RUNLEVEL" in
/etc/init.d/kexec:        if  "$RUNLEVEL" -eq 6 ] ; then
/etc/init.d/powerfail:  test "$RUNLEVEL" != "S" && exit 0
/etc/init.d/rc:trap 'test "$RUNLEVEL" = "1" && exit 0' SIGTERM
/etc/init.d/rc:test "$PREVLEVEL" = "$RUNLEVEL" && exit 0
/etc/init.d/rc:test "$PREVLEVEL" = "1" -a "$RUNLEVEL" = "S" && exit 0
/etc/init.d/rc:runrc=/etc/init.d/rc${RUNLEVEL}.d
/etc/init.d/rc:. /etc/rc.splash $RUNLEVEL
/etc/init.d/rc:    if test "$RUNLEVEL" != "0" -a "$RUNLEVEL" != "6" ; then
/etc/init.d/rc:if test "$RUNLEVEL" != "S" ; then
/etc/init.d/rc:    if test "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "6" ; then
/etc/init.d/rc:echo -e "switching to runlevel: ${stat}${extd}${RUNLEVEL}${norm}"
/etc/init.d/rc:splashtrigger "rlchange $RUNLEVEL"
/etc/init.d/rc:    eval $(startpar $startopt -M stop  -P $PREVLEVEL -R $RUNLEVEL)
/etc/init.d/rc:case "$RUNLEVEL" in
/etc/init.d/rc:    eval $(startpar $startopt -M start -P $PREVLEVEL -R $RUNLEVEL)
/etc/init.d/rc:splashtrigger "rlreached $RUNLEVEL"
/etc/init.d/rc:echo -e "runlevel ${RUNLEVEL} has been ${stat}${extd}reached${norm}"
/etc/init.d/rc:    echo -n  "Failed services in runlevel ${RUNLEVEL}: "
/etc/init.d/rc:    echo -n  "Skipped services in runlevel ${RUNLEVEL}: "
/etc/init.d/single:     if test -n "$RUNLEVEL" -a "$RUNLEVEL" != "S" ; then
/etc/init.d/skeleton.compat:    if test -z "$RUNLEVEL"; then read RUNLEVEL REST < <(/sbin/runlevel); fi

Well, it was not needed to post the last output. I only gave you that one to see for yourself, I did allready know that RUNLEVEL is used in a lot of places. :wink:

And what you have looks nice to me. Only thing is that when your mountmypart.sh script is that short, the only one statement you have can equaly go directly in after.local:

#!/bin/bash
test "$RUNLEVEL" = "5" && mount /home/myuser/Documents/Documents-Mounted

But the fact that you do not tell something like: “it works, thank you” led me to think that it still does not do what you want. Is that true?

In that case you can do a few things:

  1. check if your script is called, by adding e.g.
date >/tmp/mountmypart-log

to it. This will create the file* /tmp/mountmypart-log* with the date/time when the script was called last.

  1. are you sure that mount is correct. Did you check the entry in* /etc/fstab* that belongs to it?

I would like to thank you again for your kindness to help me.
Unfortunately it does not work. I will post back on Monday when I will be back at office

Hmm I have corrected one think
I added the execution permission to my file. It was not executed before but now it is . The date >/tmp/…you suggested me worked nice

The problem I have now is that the file didnot mount my partition…
Strange is that if I am root and I execute the bash file on my own with sh /root/mountme.sh the partition is mounted.

I tried also in my bash file

mount /home/myuser/Documents/Documents-server > /tmp/whatwentwrong.txt to redirect output but I didnt see anythin going wrong.

What should I try out now?

Thanks a lot

alaios wrote:

> mount /home/myuser/Documents/Documents-server > /tmp/whatwentwrong.txt
> to redirect output but I didnt see anythin going wrong.
>
> What should I try out now?

mount -v shows you more about what it was trying to do

On 2011-01-30 21:06, jdmcdaniel3 wrote:
>
> I am not an expert on what scripts are allowed while in runlevel 3, but
> you could add a symlink to your script to be run while in rl3 like this
> in a terminal session:

Warning: init script symlinks are not guaranteed to run in a *SUSE system.


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” at Telcontar)

This is what I got when my script was executed

mount from util-linux-ng 2.17.2 (with libblkid and selinux support)

produced by:

mount -V /home/user/Documents/Documents-server > /tmp/whatwentwrong.txt

Hello as you can see from my last post. It seems that my file is executed at run time. Still I do not get any errors.

Do not you think that I capture correctly any errors when they happen?

What else should I check? Still I do not get my mount mounted.

Best Regards
Alex

On 2011-02-18 16:06, alaios wrote:
>
> This is what I got when my script was executed
>
> mount from util-linux-ng 2.17.2 (with libblkid and selinux support)
>
>
> produced by:
>
> mount -V /home/user/Documents/Documents-server > /tmp/whatwentwrong.txt

You were told to use “-v”, not “-V”. Attention to detail is crucial.


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” at Telcontar)

changed it to -v as you corrected me and now things work :slight_smile: