Operating System - Linux
1752482 Members
5927 Online
108788 Solutions
New Discussion юеВ

Re: Doesn't execute the command in my init script?

 
SOLVED
Go to solution
Karthik Dathathri_1
Occasional Contributor

Doesn't execute the command in my init script?

Hello All,

I wrote the following script and saved it in /etc/init.d/ directory as jabber_init:

#!/bin/bash
#
# Setting Configuration
#
# chkconfig: 3 98 1
# description: Setting Jabber Configuration
#

start() {
/bin/touch /root/karthik.txt
/bin/chmod 777 /root/karthik.txt
}

stop() {
exit 0;
}
and added it to boot-up sequence using the following steps:
1. chkconfig jabber_init --add
which created file called S98jabber_init in /etc/rc3.d folder
2. chkconfig --list jabber_init
jabber_init 0:off 1:off 2:off 3:on 4:off 5:off 6:off
3. The file permissions of /etc/init.d/jabber_init is below:
ll /etc/init.d/jabber_init
-rwxr-xr-x 1 root root 229 Jun 17 20:36 /etc/init.d/jabber_init

When I rebooted the machine, syslog told that jabber_init succeeded:
Jun 17 20:39:15 symlin5 rc: Starting jabber_init: succeeded

But the file /root/karthik.txt didn't got created and the chmod command didn't worked.

Any clues/pointers would be helpful. I'm a newbie to SysAdmin. I checked the internet, googled a bit, but couldn't get any clues.

The system I am using is:
Red Hat Enterprise Linux AS release 3 (Taroon Update 3)

Thanks & Regards,
Karthik D

8 REPLIES 8
Fred Ruffet
Honored Contributor

Re: Doesn't execute the command in my init script?

Could you provide result of :
ls -l /etc/rc3.d/S98jabber_init

Also, you could redirect output at end of lines :
/bin/touch /root/karthik.txt >>/tmp/logfile 2>&1
/bin/chmod 777 /root/karthik.txt >>/tmp/logfile 2>&1
It will show you in /tmp/logfile what's happening (if something happens). Maybe at execution time, script doesn't have permission to write to /root (it may be launched by bin user, not by root).

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Steven E. Protter
Exalted Contributor

Re: Doesn't execute the command in my init script?

I don't see a PATH variable set.

If you don't set a PATH variable, the command you think should be executing may not be executing, leading to no file creation.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Karthik Dathathri_1
Occasional Contributor

Re: Doesn't execute the command in my init script?

Fred:
Thanks for your reply.

1. ls -l /etc/rc3.d/S98jabber_init
lrwxrwxrwx 1 root root 21 Jun 17 20:36 /etc/rc3.d/S98jabber_init -> ../init.d/jabber_init

2. I did the changes to the script. But nothing got writtent to /tmp/logfile.
ls -l for tmp
drwxrwxrwt 6 root root 4096 Jun 17 22:25 tmp

3. Below are contents of /tmp
total 8
-rwxrwxrwx 1 root root 0 Jun 17 22:22 logfile
drwx------ 2 root root 4096 Oct 15 2004 orbit-root
drwx------ 2 root root 4096 Oct 15 2004 ssh-hPue3517

I created the logfile in temp using touch /tmp/logfile and did chmod 777 logfile before rebooting the system.

SEP:
What PATH variable needs to be set? I don't understand.

Thanks & Regards,
Karthik D
Gopi Sekar
Honored Contributor

Re: Doesn't execute the command in my init script?

Try adding the following to your script and see whether it solves your problem


. /etc/init.d/functions

remember the beginning dot is important

Hope this helps,
Gopi
Never Never Never Giveup
xyko_1
Esteemed Contributor
Solution

Re: Doesn't execute the command in my init script?

Hi Karthik,

the way you have written your scrit it only has two routines but no one are been called, so ... nothing are executed.

To have the start routine executed you must put a line at the end just like that

start

Please, if you are a novice in linux and script language and you do not have time to study before implement what you need, try to look a script that exist on /etc/init.d and have it as an example.

regards,
xyko
Karthik Dathathri_1
Occasional Contributor

Re: Doesn't execute the command in my init script?

Hi xyko,
Thanks for your time and reply. Your suggestion really worked. Thanks once again to this forum members for helping newbies like me.

As an acknowledgement, I had assigned 10 points to xyko.

Thanks & Regards,
Karthik
Dave Falloon
Trusted Contributor

Re: Doesn't execute the command in my init script?

A better solution is to use a case statement ( or chain of if statements ) to determine whether the arguments passed to your script are proper and then make the correct choice after that. If you just call the bash functions within your script, ie:

start()
{ touch /tmp/foo }
stop()
{ rm /tmp/foo }

start
stop

then regardless of the arguments passed to the script you will run both of the functions. Add lines like this after your functions to make the your script choose the correct action:

case "$1" in
start)
start
;;
stop)
stop
;;
*)
echo 'Usage: scriptname (start|stop)'
esac

Most init scripts are setup similarly.

--Dave
Clothes make the man, Naked people have little to no effect on society
xyko_1
Esteemed Contributor

Re: Doesn't execute the command in my init script?

Hi Everybody,

I didn't say to Karthik that putting a start command at the end of the script will correct the script at all. It was only a way to show him why nothing was happening when the script was executed. At the end of my post I suggest him to look at a script in /etc/init.d to see how to write a script to initiate some kind of service because I understood that he does not have good knowledge of script language.

Regards to all,
Xyko