1

I have a script /etc/init.d/startup, where I do the following:

  • creating a PID file that should look like: /var/run/**startup**.pid
  • executing a screen of a java process, so I will like to create the screen with the name startup (like this screen -dmS startup "path to a script"

I don't have any problems with the any of the above while the server is running, but when it boots up it creates the PIDfile and screen with the incorrect names like this:

/var/run/**S92startup**.pid

2058.**S92startup** (11/10/2014 03:56:31 PM)    (Detached)

How can i assign the name of the script while booting? (not the name of the symlink in /etc/rc2.d/)

Right now my script looks like this at the beginning and this is how I'm getting the name of the script

SCRNAME=${0##*/}

DAEMON="screen -DmS $SCRNAME /srv/startup/scripts/gprs.sh"

PIDFILE=/var/run/$SCRNAME.pid

PS: it is working right when i'm on the server and execute "service startup start" but not when booting.

janos
  • 798
  • 1
  • 5
  • 22
mk9
  • 11
  • 1
  • It's the way `init` does its processing. When booting it assigns a "priority" to those items. Use `/etc/rc.local` instead. – Nathan C Nov 10 '14 at 23:59
  • 2
    It is the `$0` that is your problem. It is going to give you the name of the symlink when the symlink is used to start the script. Perhaps you should do something like `readlink -f $0` to resolve the symbolic name to the real path? – Zoredache Nov 11 '14 at 00:13

1 Answers1

0

The problem is that when the script is invoked by a symlink, $0 is set to the symlink not the original script.

You can do this:

SCRNAME=$(readlink "$0" || echo $0)
SCRNAME=${SCRNAME##*/}

The readlink will print the link target if the source is a link.

janos
  • 798
  • 1
  • 5
  • 22