Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script called from inittab cannot find files in current directory 3

Status
Not open for further replies.

svar

Programmer
Aug 12, 2001
349
0
0
GR
I have a working script(in perl) that runs fine in /home/me/mydirectory
Now since this should be running continously with no administration,I want this to run automatically if say the computer shuts down and is restarted. So I put in /etc/inittab
myjob:3456:respawn:/home/me/scriptcaller

which says
#!/bin/bash
cd /home/me/mydirectory
su - me -c "perl /home/me/mydirectory/myscript.pl"

The script runs, except that it cannot find data files like
myfile in /home/me/mydirectory/
-by opening and writing to a file, I confirmed that it only looks in /home/me

I thought I hqad taken care of this by putting
/home/me/mydirectory in $PATH, both in /etc/profile and
in .bashrc

What am I missing here?
 
Not sure, but it's probably better to define the $PATH in the script itself, so that there's no abiguity at all.

I want to be good, is that not enough?
 
When you "su -", you are dropped into your home directory.

what you need to do is move the cd into the su-ed comand. Try this (the stuff within quotes should be all one line, or break it up with backslashes):

Code:
#!/bin/bash
su - me -c "cd /home/me/mydirectory; perl /home/me/mydirectory/myscript.pl"

Cheers!
 
No, I tried putting it[export PATH=$PATH:\home\me\mydirectory] both in the perl script and in the script called by init, /home/me/scriptcaller
but it still looks only in /home/me


 
Thanks jgunkel, that did it! Now my only problem is that
even though I put a & at the end of the script, I get the screen filled with prints, so it's hard to even log on...
 
Why put it in inittab?

Why not put your commands in rc*.d directory?
 
okay then, stuff " > /dev/null " without the quotes immediately before the"&".

Alternatively, if you need to see the output, you could take advantage of init's ability to bind a process to a particular, unused, tty.

As far as putting it in inittab, there's two reasons: 1) you can take advantage of init's ability to respawn tasks and 2) not every unix-y platform has SYSV style rc scripts

That being said, rc scripts are certainly more convenient when you have that facility on your system.
 
Ok, just for the record, how would you do this with rc scripts?
 
With the >/dev/null thing it almost works, except that it still writes to the screen(before I can log in) when it finds
that it would be overwriting a file it is supposed to gunzip, which is a disaster, as it respawns the script a zillion times, effectively this puts the system to a stall. Strangely enough when I ran from the command line, I do not get this problem.
I need to look at this, probaby use script .... exit
to get a trace in typescript file
 
Ok, it runs now, except it respawns all the time,
so if you do a ps,
you see a growing list of
perl /home/me/mydirectory/myscript.pl

which eventually brings the system to a stall
I fixed all the previous(permission) problems, so that
the original script does not die, but I do not understand why a new one is respawned all the time with PPID 1, i.e. the init. If I understand right,
myscr:36:respawn:myscript

will start the script once when any of levels 3(multiuser)
or 6(reboot) are reached[not sure I need this], not as long as we are in levels 3 or 6. Or did I get this wrong?
 
Ok, I guess the problem is that the shell script calls
perl myscript.pl and then dies and is thus respawned(the shell script) by init, even though its child (the perl script) is running... So the shell script must check
first and only start perl if the perl script is not running
 
Ah yes!

if you get rid of the ampersand again, then your shellscript will stay in the foreground, and not detatch from init. init then, will only restart the script when it exits. The good thing about running from init is that your process will be restarted exerytime it exits. The bad thing is that it will be restarted everytime it exits. :)

 
This might be a good time to mention DJB's daemontools

It's dead easy to install and is a far better watchguard for this kind of thing than init is... IMHO



Daemontools gets installed with most qmail howtos, but it's perfectly capable of managing any reasonably scripted animal that needs to stay up. Give it some consideration and get outta inittab.


D.E.R. Management - IT Project Management Consulting
 
Yes, so I put in the shell script:
su - me -c "cd /home/me/mydirectory;
if !(ps ax | grep -v grep | grep myscript.pl >/dev/null)
then
perl /home/me/mydirectory/myscript.pl > /dev/null &
fi"

I get messages about this respawning too fast (wish I could tell init not too), but it works fine. Respawning does
not excecute the if block unless it really has to
 
Agreed.

If you are not restricted by your environment, daemontools is definitely the way to go for all those "I have a script that needs to..." sort of tasks.

I had actually forgot about it. It's been a couple of years since I have needed to use it.

(goes off to look at cr.yp.to for a while...)
 
svar,

Get rid of the "&", and the task will block until it is ready to exit or dies.

This is the behavior that init expects processes to exhibit.
 
Well, getting rid of the & is probably not a good idea here,
since this is a server running unattended without X, so
if you get rid of the &, user "me" has no shell to login(one could always login as root though, but not from telnet)
 
Incidentally on Slack 12, I could not make daemontools work:


Script started on Fri 26 Oct 2007 01:07:57 PM EEST
root@quality5:/package# ls -l
total 40
drwxr-xr-t 3 root root 80 2001-07-12 19:49 admin
-rw-r--r-- 1 root root 36975 2007-10-26 13:03 daemontools-0.76.tar.gz
-rw-r--r-- 1 root root 0 2007-10-26 13:07 typescript
root@quality5:/package# pwd
/package
root@quality5:/package# rm daemn on*
root@quality5:/package# cd admin
root@quality5:/package/admin# cd daemontools-0.76
root@quality5:/package/admin/daemontools-0.76# ls
package src
root@quality5:/package/admin/daemontools-0.76# ls -l
total 5
drwxr-xr-x 2 root root 368 2001-07-12 19:49 package
drwxr-xr-x 2 root root 5232 2001-07-12 19:49 src
root@quality5:/package/admin/daemontools-0.76# package/install
Linking ./src/* into ./compile...
Compiling everything in ./compile...
sh find-systype.sh > systype
rm -f compile
sh print-cc.sh > compile
chmod 555 compile
./compile byte_chr.c
./compile byte_copy.c
./compile byte_cr.c
./compile byte_diff.c
./compile byte_rchr.c
./compile fmt_uint.c
./compile fmt_uint0.c
./compile fmt_ulong.c
rm -f makelib
sh print-ar.sh > makelib
chmod 555 makelib
./compile scan_ulong.c
./compile str_chr.c
./compile str_diff.c
./compile str_len.c
./compile str_start.c
./makelib byte.a byte_chr.o byte_copy.o byte_cr.o byte_diff.o \
byte_rchr.o fmt_uint.o fmt_uint0.o fmt_ulong.o scan_ulong.o str_chr.o \
str_diff.o str_len.o str_start.o
rm -f choose
cat warn-auto.sh choose.sh \
| sed s}HOME}"`head -1 home`"}g \
> choose
chmod 555 choose
./choose c trydrent direntry.h1 direntry.h2 > direntry.h
./compile envdir.c
rm -f load
sh print-ld.sh > load
chmod 555 load
./compile alloc.c
./compile alloc_re.c
./compile buffer.c
./compile buffer_0.c
./compile buffer_1.c
./compile buffer_2.c
./compile buffer_get.c
./compile buffer_put.c
./compile buffer_read.c
./compile buffer_write.c
./compile coe.c
./compile env.c
./compile error.c
./compile error_str.c
./compile fd_copy.c
./compile fd_move.c
./choose cl trymkffo hasmkffo.h1 hasmkffo.h2 > hasmkffo.h
./compile fifo.c
./choose cl tryflock hasflock.h1 hasflock.h2 > hasflock.h
./compile lock_ex.c
./compile lock_exnb.c
./compile ndelay_off.c
./compile ndelay_on.c
./compile open_append.c
./compile open_read.c
./compile open_trunc.c
./compile open_write.c
./compile openreadclose.c
./compile pathexec_env.c
./compile pathexec_run.c
pathexec_run.c: In function 'pathexec_run':
pathexec_run.c:18: warning: implicit declaration of function 'execve'
./compile chkshsgr.c
chkshsgr.c: In function 'main':
chkshsgr.c:10: warning: passing argument 2 of 'getgroups' from incompatible pointer type
chkshsgr.c:10: warning: implicit declaration of function 'setgroups'
./load chkshsgr
./chkshsgr || ( cat warn-shsgr; exit 1 )
./choose clr tryshsgr hasshsgr.h1 hasshsgr.h2 > hasshsgr.h
./compile prot.c
prot.c: In function 'prot_gid':
prot.c:13: warning: implicit declaration of function 'setgroups'
prot.c:15: warning: implicit declaration of function 'setgid'
prot.c: In function 'prot_uid':
prot.c:20: warning: implicit declaration of function 'setuid'
./compile readclose.c
./compile seek_set.c
seek_set.c: In function 'seek_set':
seek_set.c:9: warning: implicit declaration of function 'lseek'
./compile sgetopt.c
./compile sig.c
./choose cl trysgprm hassgprm.h1 hassgprm.h2 > hassgprm.h
./compile sig_block.c
./choose cl trysgact hassgact.h1 hassgact.h2 > hassgact.h
./compile sig_catch.c
./compile sig_pause.c
./compile stralloc_cat.c
./compile stralloc_catb.c
./compile stralloc_cats.c
./compile stralloc_eady.c
./compile stralloc_opyb.c
./compile stralloc_opys.c
./compile stralloc_pend.c
./compile strerr_die.c
./compile strerr_sys.c
./compile subgetopt.c
./choose cl trywaitp haswaitp.h1 haswaitp.h2 > haswaitp.h
./compile wait_nohang.c
./compile wait_pid.c
./makelib unix.a alloc.o alloc_re.o buffer.o buffer_0.o buffer_1.o \
buffer_2.o buffer_get.o buffer_put.o buffer_read.o buffer_write.o \
coe.o env.o error.o error_str.o fd_copy.o fd_move.o fifo.o lock_ex.o \
lock_exnb.o ndelay_off.o ndelay_on.o open_append.o open_read.o \
open_trunc.o open_write.o openreadclose.o pathexec_env.o \
pathexec_run.o prot.o readclose.o seek_set.o sgetopt.o sig.o \
sig_block.o sig_catch.o sig_pause.o stralloc_cat.o stralloc_catb.o \
stralloc_cats.o stralloc_eady.o stralloc_opyb.o stralloc_opys.o \
stralloc_pend.o strerr_die.o strerr_sys.o subgetopt.o wait_nohang.o \
wait_pid.o
./load envdir unix.a byte.a
/usr/lib/gcc/i486-slackware-linux/4.1.2/../../../../i486-slackware-linux/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference in envdir.o
/lib/libc.so.6: could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [envdir] Error 1
Copying commands into ./command...
cp: cannot stat `compile/svscan': No such file or directory
root@quality5:/package/admin/daemontools-0.76# exit

Script done on Fri 26 Oct 2007 01:09:01 PM EEST
 
The thing is, that when init runs the program, it expects that program to stay attached and in the foreground. That's how it knows if it needs to restart the program.

So to put it another way, sending the job to the background is desirable behavior for when you are developing this application, but when it runs from init, the program must remain in the foreground so that init can tell when the program terminates (and therefore needs to be restarted)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top