Solved

Inconsisten shell behavior using cron

Posted on 1998-10-23
11
343 Views
Last Modified: 2013-12-23
I'm on DG AViiON DG/UX 5.11
I noticed some inconsistencies in the execution of a shell script. When running it from the system prompt, all variables are properly set. However, if I schedule it through cron, the same variables are not set. Here is my script:
#!/bin/ksh
cat t1 | while read line
do
   echo $line
   eval $line
done
echo "a: $a"

The contents of file t1 is:
a=test
export a

When I run my script from the system prompt, the last echo command shows:
a: test
The same script run by cron shows:
a:
How do I make it consistent?
0
Comment
Question by:mgokman
  • 5
  • 3
  • 3
11 Comments
 
LVL 1

Accepted Solution

by:
MichaelKrastev earned 50 total points
ID: 1582494
Cron demon starts /bin/sh to execute scripts. It doesn't pay
attention to your #!/bin/sh in the first line of the script. This way you have ksh from the sysconsole (if this is your shell or because you explicitely call /bin/ksh) and /bin/sh from the cron. If you want to make sure cron uses ksh to execute your script, prepend /bin/ksh in the definition in your crontab entry, e.g. /bin/ksh <script_name>


0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1582495
MichaelKrastev, well cron uses /bin/sh and this knows what to do with #! in the very first line ;-)
The Problem is the echo command in the while loop: usually in a cron you cannot perform commands using stdout, stderr without redirecting the output.
0
 
LVL 1

Expert Comment

by:MichaelKrastev
ID: 1582496
Well, /bin/sh knows what to do with the magic number #! in the very first line, but it doesn't always want to do so. When is it willing to use it -- your are invoking a program and the shell must determine whether it is a standalone executable or interpretable script, and in the later case what interpreter to use. However, when you explicitely invoke /bin/sh and pass it script that begins with #!/bin/ksh, that /bin/sh process will not exec /bin/ksh to do the job. And it makes sense, if you start /bin/sh then the UNIX understands that this is exactly what you want.

Back to the question. The problem is the variable assignment or expression evaluation, I guess. It has nothing to do with the cron, because it behaves exactly the same way if you run the example manually, first with sh and then with ksh.

All mdokman wants is to have his script work consistently. And this is what I gave him -- run /bin/ksh <script> from the cron.

0
 
LVL 3

Author Comment

by:mgokman
ID: 1582497
Hi MichaelKrastev,
Once I put /bin/ksh at the beginning of the line in my crontab, everything started working the way I wanted.
I noticed quite a few times that certain things work differently on my platform depending on what shell is used, sh or ksh. Also, I'm not sure if eval is available in sh, so when it ran by sh, maybe my variable assignments didn't work. Or, it could be something else. One thing I know for sure using /bin/ksh to call my script from cron makes it work exactly the same way as calling it from the sstem prompt and that's what I needed.
Thanks a lot.
0
 
LVL 3

Author Comment

by:mgokman
ID: 1582498
After reading ahoffmann's comment, I want to mention that the script I gave in my question is certainly no the one I raly had troubles with. This was just a simplified example. My goal realy was to make use of the eval command. I have script that accepts many parameters. Instead of passing them on the command line, I decided to implement something similar to .ini files in Windows environment. This way I can put as many assignments as I want into a file myfile.ini. Then I can loop throught this file and use eval like in my example to make all variable assignments. The .ini file name will be the only parameter for my script. So, the echo command in my script is just for illustartive purpose, the important command is eval. Also, echo works fine no matter how I execute my script. My problem was that I was not getting all variables assignments when running it via cron. Now it works fine. Thanks to MichaelKrastev.
0
Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

 
LVL 1

Expert Comment

by:MichaelKrastev
ID: 1582499
I confess, I can'n explain what is going on inside /bin/sh and /bin/ksh during the processing of eval. However, on Solaris (2.5.1) if you take a look at the end of the man page for eval, there is some vague explanation about the behaviour of eval under /bin/ksh. After reading it 10 minutes, I couldn't understand it. You may give a try.

0
 
LVL 3

Author Comment

by:mgokman
ID: 1582500
I just noticed that since I changed my cron to use /bin/ksh I started getting slightly different output in my script log.
I have the following in my crontab:
00 20 * * 1-4 /bin/ksh /path/myscript 2>&1 > /path/mylog
Within myscript I call various shell scripts and Oracle sql scripts. They all write their output to standard output. However, I don't see errors in the log file. I also don't see the output from dd command that copies files. Now this output goes to my mail.
Any suggestions how to put them all back to the log file. I could just use redirect inside my scripts, but I want to avoind it.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1582501
00 20 * * 1-4 /bin/ksh /path/myscript > /path/mylog 2>&1
0
 
LVL 3

Author Comment

by:mgokman
ID: 1582502
ahoffman,
following your example I changed my cron and now I see all output in my log file, no more errors in mailx. I'm not sure I understand the difference in my syntax and yours, although I read about it in a Unix book.
You deserve point for this answer, but I think I ran out of my points, so I need to check my balance. If I still have them I'll repost this question. You can answer it and I will award you points. I'd appreciate if you provide some explanation why my syntax produces different results.
Thanks a lot to you both.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1582503
from man sh:
--
  The order in which redirections are  specified  is  signifi-
  cant.   The shell evaluates redirections left-to-right.  For
  example:
      ... 1>xxx 2>&1
  first associates file descriptor 1 with file xxx.  It  asso-
  ciates  file descriptor 2 with the file associated with file
  descriptor 1 (that is, xxx).  If the order  of  redirections
  were  reversed,  file  descriptor 2 would be associated with
  the terminal (assuming file descriptor 1 had been) and  file
  descriptor 1 would be associated with file xxx.
--

This is one of the reason why I prefere csh and friends, where
this simply is written as:

     ... >& xxx
0
 
LVL 3

Author Comment

by:mgokman
ID: 1582504
I think I understand now. I think I confused file descriptors with actual files.
Thanks a lot.
0

Featured Post

New My Cloud Pro Series - organize everything!

With space to keep virtually everything, the My Cloud Pro Series offers your team the network storage to edit, save and share production files from anywhere with an internet connection. Compatible with both Mac and PC, you're able to protect your content regardless of OS.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Xen 6.1, 6.2 Poor NIC performance in ShadowProtect/WinPE 9 80
New VoIP phone system - what networking changes should be made 4 102
PCI Compliance Free scan 2 79
DHCP Server 14 67
This is an article about my experiences with remote access to my clients (so that I may serve them) and eventually to my home office system via Radmin Remote Control. I have been using remote access for over 10 years and have been improving my metho…
PRTG Network Monitor lets you monitor your bandwidth usage, so you know who is using up your bandwidth, and what they're using it for.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now