LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   script in /etc/init.d not working (https://www.linuxquestions.org/questions/programming-9/script-in-etc-init-d-not-working-885908/)

Annielover 06-12-2011 11:01 AM

script in /etc/init.d not working
 
Here's something I really don't understand: I wrote a script to start a service at start up and put it in /etc/init.d

Then I ran "sudo update-rc.d SCRIPT defaults" and it all went good, no errors.

However, when I reboot the server, the process is not running...
When I do "sudo /etc/init.d/SCRIPT start", still nothing happens...

I made a copy of that script in my home directory and ran it from there: perfect!

After that, when I do "sudo /etc/init.d/SCRIPT start", it does run with no errors!

very very strange, what am I doing wrong here?

David the H. 06-12-2011 11:51 AM

Quote:

Originally Posted by Annielover (Post 4383419)
what am I doing wrong here?

Not showing us the actual script so we can possibly determine what the problem is, perhaps? ;)

Annielover 06-12-2011 12:26 PM

Code:

# !/bin/sh
# /etc/init.d/COD4
# Script to start the cod4 servers

### BEGIN INIT INFO
# Provides:          scriptname
# Required-Start:    $remote_fs $syslog
# Required-Stop:    $remote_fs $syslog
# Default-Start:    2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Call of Duty 4 at boot time
# Description:      ...
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME="COD4"
PATHTOCOD4="/home/laurent/Games/Cod4"

case "$1" in
start)
echo "starting COD4..."
/home/laurent/Games/Cod4/cod4_lnxded_MIX +set dedicated 2 +set sv_punkbuster 0 +set net_port 28961 +exec serverMIX.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
/home/laurent/Games/Cod4/cod4_lnxded_TDM +set dedicated 2 +set sv_punkbuster 0 +set net_port 28962 +exec serverTDM.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
/home/laurent/Games/Cod4/cod4_lnxded_FFA +set dedicated 2 +set sv_punkbuster 0 +set net_port 28963 +exec serverFFA.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
/home/laurent/Games/Cod4/cod4_lnxded +set dedicated 2 +set sv_punkbuster 0 +exec server.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
echo "`date +"%A %d %B %Y, %H:%M:%S"` -->  Call of Duty 4 servers started." >> $PATHTOCOD4/Serverlog.log
echo "COD4 started."
;;
stop)
echo "stopping COD4..."
pkill cod4_lnxded
echo "`date +"%A %d %B %Y, %H:%M:%S"` -->  Call of Duty 4 servers stopped." >> $PATHTOCOD4/Serverlog.log
echo "COD4 stopped."
;;
restart)
echo "restarting COD4..."
pkill cod4_lnxded
echo "`date +"%A %d %B %Y, %H:%M:%S"` -->  Call of Duty 4 servers stopped." >> $PATHTOCOD4/Serverlog.log
/home/laurent/Games/Cod4/cod4_lnxded_MIX +set dedicated 2 +set sv_punkbuster 0 +set net_port 28961 +exec serverMIX.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
/home/laurent/Games/Cod4/cod4_lnxded_TDM +set dedicated 2 +set sv_punkbuster 0 +set net_port 28962 +exec serverTDM.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
/home/laurent/Games/Cod4/cod4_lnxded_FFA +set dedicated 2 +set sv_punkbuster 0 +set net_port 28963 +exec serverFFA.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
/home/laurent/Games/Cod4/cod4_lnxded +set dedicated 2 +set sv_punkbuster 0 +exec server.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
echo "`date +"%A %d %B %Y, %H:%M:%S"` -->  Call of Duty 4 servers started." >> $PATHTOCOD4/Serverlog.log
echo "COD4 restarted."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart}"
exit 1
;;
esac

exit 0


catkin 06-12-2011 01:38 PM

It does sound very strange. If the problem persists, and the script does not run at the next boot, try
Code:

sudo /bin/sh -x /etc/init.d/SCRIPT start
to generate some tracing.

BTW it's easier to read scripts and output on LQ when they are in code tags (that's a link to instructions or it may be easier to use "Advanced Edit" mode which has a # button for code tags).

Annielover 06-12-2011 02:23 PM

Okay, that's the output:

Code:

laurent@server:~$ ps -e | grep cod4
laurent@server:~$ sudo /bin/sh -x /etc/init.d/COD4 start
[sudo] password for laurent:
+ PATH=/sbin:/bin:/usr/sbin:/usr/bin
+ NAME=COD4
+ PATHTOCOD4=/home/laurent/Games/Cod4
+ echo starting COD4...
starting COD4...
+ /home/laurent/Games/Cod4/cod4_lnxded_MIX +set dedicated 2 +set sv_punkbuster 0                                                                                                                                +set net_port 28961 +exec serverMIX.cfg +map_rotate
+ /home/laurent/Games/Cod4/cod4_lnxded_FFA +set dedicated 2 +set sv_punkbuster 0                                                                                                                                +set net_port 28963 +exec serverFFA.cfg +map_rotate
+ /home/laurent/Games/Cod4/cod4_lnxded +set dedicated 2 +set sv_punkbuster 0 +ex                                                                                                                              ec server.cfg +map_rotate
+ /home/laurent/Games/Cod4/cod4_lnxded_TDM +set dedicated 2 +set sv_punkbuster 0                                                                                                                                +set net_port 28962 +exec serverTDM.cfg +map_rotate
+ date +%A %d %B %Y, %H:%M:%S
+ echo Sunday 12 June 2011, 21:09:12 -->  Call of Duty 4 servers started.
+ echo COD4 started.
COD4 started.
+ exit 0
laurent@server:~$ ps -e | grep cod4
laurent@server:~$

But my copy of that script (without the start, stop, restart ofc) is located in /home/laurent/Games/Cod4.
When my pwd is
Code:

/home/laurent
and I type
Code:

Games/Cod4/bash StartCod4.sh
nothing happens.
When I'm in
Code:

/home/laurent/Games/Cod4
and type
Code:

bash StartCod4.sh
it does run.
Again, when my current dir is
Code:

/home/laurent
and I type
Code:

/etc/init.d/COD4 start
nothing happens.
But when my cur dir is
Code:

/home/laurent/Games/Cod4
and then I type
Code:

/etc/init.d/COD4 start
it does run perfect...

So only if my current directory is /home/laurent/Games/Cod4 my script in /etc/init.d runs...
Weird, isn't it?

catkin 06-12-2011 02:38 PM

Presumably something in the /home/laurent/Games/Cod4/cod4_lnxded* commands depends on the current working directory. Do they use envars? Maybe PATHTOCOD4? If so, changing /etc/init.d/COD4 to export PATHTOCOD4=/home/laurent/Games/Cod4 would fix it. Alternatively, how about modifying /etc/init.d/COD4 to include cd /home/laurent/Games/Cod4 ?

Annielover 06-13-2011 01:57 AM

Adding export to $PATHTOCOD4 did not work,

That's how my script looks now:

Code:

# !/bin/sh
# /etc/init.d/COD4
# Script to start the cod4 servers

### BEGIN INIT INFO
# Provides:          scriptname
# Required-Start:    $remote_fs $syslog
# Required-Stop:    $remote_fs $syslog
# Default-Start:    2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Call of Duty 4 at boot time
# Description:      ...
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=COD4
export PATHTOCOD4=/home/laurent/Games/Cod4

case "$1" in
start)
echo "starting COD4..."
cd $PATHTOCOD4/cod4_lnxded_MIX +set dedicated 2 +set sv_punkbuster 0 +set net_port 28961 +exec serverMIX.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
cd $PATHTOCOD4/cod4_lnxded_TDM +set dedicated 2 +set sv_punkbuster 0 +set net_port 28962 +exec serverTDM.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
cd $PATHTOCOD4/cod4_lnxded_FFA +set dedicated 2 +set sv_punkbuster 0 +set net_port 28963 +exec serverFFA.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
cd $PATHTOCOD4/cod4_lnxded +set dedicated 2 +set sv_punkbuster 0 +exec server.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
echo "`date +"%A %d %B %Y, %H:%M:%S"` -->  Call of Duty 4 servers started." >> $PATHTOCOD4/Serverlog.log
echo "COD4 started."
;;
stop)
echo "stopping COD4..."
pkill cod4_lnxded
echo "`date +"%A %d %B %Y, %H:%M:%S"` -->  Call of Duty 4 servers stopped." >> $PATHTOCOD4/Serverlog.log
echo "COD4 stopped."
;;
restart)
echo "restarting COD4..."
pkill cod4_lnxded
echo "`date +"%A %d %B %Y, %H:%M:%S"` -->  Call of Duty 4 servers stopped." >> $PATHTOCOD4/Serverlog.log
cd $PATHTOCOD4/cod4_lnxded_MIX +set dedicated 2 +set sv_punkbuster 0 +set net_port 28961 +exec serverMIX.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
cd $PATHTOCOD4/cod4_lnxded_TDM +set dedicated 2 +set sv_punkbuster 0 +set net_port 28962 +exec serverTDM.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
cd $PATHTOCOD4/cod4_lnxded_FFA +set dedicated 2 +set sv_punkbuster 0 +set net_port 28963 +exec serverFFA.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
cd $PATHTOCOD4/cod4_lnxded +set dedicated 2 +set sv_punkbuster 0 +exec server.cfg +map_rotate >> $PATHTOCOD4/Serverlog.log 2> $PATHTOCOD4/Serverlog.err < /dev/null&
echo "`date +"%A %d %B %Y, %H:%M:%S"` -->  Call of Duty 4 servers started." >> $PATHTOCOD4/Serverlog.log
echo "COD4 restarted."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart}"
exit 1
;;
esac

exit 0

And in serverlog.err:

Code:

cd: 51: can't cd to /home/laurent/Games/Cod4/cod4_lnxded_FFA
But when I remove cd, Serverlog.err looks like:
Code:

CoD4 MP 1.7 build linux-i386 Jun 28 2008
begin $init
----- FS_Startup -----
Sys_Error: Error during initialization:
No IWD files found in /main

But strange that cd is not working...
I specify the root folder of the game in my variable and main is located in that root folder ofc. (Cod4/main)

ssrameez 06-13-2011 06:31 AM

could you fire a "ls /home/laurent/Games/Cod4" and paste the results.

Please fire the command as root from /

Is your /home a separate partition. Is it from NFS. does root user able to cd to that directory. ?

Annielover 06-13-2011 07:41 AM

Code:

laurent@server:/$ ls -l /home/laurent/Games/Cod4
total 24788
-rw-r--r-- 1 laurent laurent  167936 2011-05-13 17:55 binkw32.dll
-rw-r--r-- 1 laurent laurent  216358 2011-05-13 17:55 cod4.ico
-rwxr-xr-x 1 laurent laurent 2278332 2011-05-14 16:10 cod4_lnxded
-rwxr-xr-x 1 laurent laurent 2277884 2008-06-04 05:36 cod4_lnxded-bin
-rwxr-xr-x 1 laurent laurent 2278332 2011-05-17 16:14 cod4_lnxded_FFA
-rwxr-xr-x 1 laurent laurent 2278332 2011-05-17 16:16 cod4_lnxded_MIX
-rwxr-xr-x 1 laurent laurent 2278332 2011-05-17 16:14 cod4_lnxded_TDM
-rw-r--r-- 1 laurent laurent 1105976 2011-05-13 17:55 cod.bmp
-rw-r--r-- 1 laurent laurent  109976 2011-05-13 17:55 codlogo.bmp
-rw-r--r-- 1 laurent laurent 3325952 2011-05-13 17:55 iw3mp.exe
-rw-r--r-- 1 laurent laurent 4498779 2011-05-13 17:55 iw3sp.exe
-rwxr-xr-x 1 laurent laurent  41384 2007-11-18 07:12 libgcc_s.so.1
-rwxr-xr-x 1 laurent laurent  939472 2007-11-18 07:12 libstdc++.so.6
-rw-r--r-- 1 laurent laurent    2482 2011-05-13 17:55 localization.txt
drwxr-xr-x 3 laurent laurent    4096 2011-05-17 16:25 main
drwxr-xr-x 2 laurent laurent    4096 2011-05-13 18:54 miles
drwxr-xr-x 3 laurent laurent    4096 2011-05-13 18:54 Mods
-rw-r--r-- 1 laurent laurent  434688 2011-05-13 17:55 mss32.dll
-rwxr-xr-x 1 laurent laurent 2539230 2007-10-24 10:18 pbsetup.run
-rw-r--r-- 1 laurent laurent    6514 2008-06-28 20:02 README.linux
-rw-r--r-- 1 laurent laurent  526490 2011-06-13 14:35 Serverlog.err
-rw-r----- 1 laurent laurent    670 2011-06-13 09:04 Serverlog.log
-rw-r--r-- 1 laurent laurent    2077 2011-06-12 21:12 Serverlog.log.1
-rwxr-xr-x 1 laurent laurent    849 2011-06-12 16:56 StartCod4.sh
-rwxr-xr-x 1 laurent laurent    154 2011-06-12 14:48 StopCod4.sh
drwxr-xr-x 3 laurent laurent    4096 2011-05-13 18:54 zone

No, my home folder is on the same partition as the game install (/dev/sda2)

catkin 06-13-2011 07:51 AM

The suggestion was to cd to the directory before (in time) running the commands so like:
Code:

start)
echo "starting COD4..."
cd $PATHTOCOD4
./cod4_lnxded_MIX +set dedicated 2 +set sv_punkbuster 0 +set net_port 28961 +exec serverMIX.cfg +map_rotate >> Serverlog.log 2>> Serverlog.err < /dev/null&
./cod4_lnxded_TDM +set dedicated 2 +set sv_punkbuster 0 +set net_port 28962 +exec serverTDM.cfg +map_rotate >> Serverlog.log 2>> Serverlog.err < /dev/null&

and similarly for the subsequent commands. The change from > to >> avoids each command overwriting the others' output in Serverlog.err

Once you get it working it would be useful to add some error trapping.

catkin 06-13-2011 07:54 AM

Quote:

Originally Posted by Annielover (Post 4383863)
Adding export to $PATHTOCOD4 did not work

Is there any documentation on the cod4_lnxded* executables and their requirements? The export was a guess that might have worked.

onebuck 06-13-2011 08:08 AM

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.

Annielover 06-13-2011 12:22 PM

Quote:

Originally Posted by catkin (Post 4384118)
The suggestion was to cd to the directory before (in time) running the commands so like:
Code:

start)
echo "starting COD4..."
cd $PATHTOCOD4
./cod4_lnxded_MIX +set dedicated 2 +set sv_punkbuster 0 +set net_port 28961 +exec serverMIX.cfg +map_rotate >> Serverlog.log 2>> Serverlog.err < /dev/null&
./cod4_lnxded_TDM +set dedicated 2 +set sv_punkbuster 0 +set net_port 28962 +exec serverTDM.cfg +map_rotate >> Serverlog.log 2>> Serverlog.err < /dev/null&

and similarly for the subsequent commands. The change from > to >> avoids each command overwriting the others' output in Serverlog.err

Once you get it working it would be useful to add some error trapping.

That worked for me!! Thank you very much!!


All times are GMT -5. The time now is 01:20 PM.