views:

95

answers:

3

I have a script that I'm trying to run from cron. When I run it from bash, it work just fine. However when I let cron do it's thing, I get a:

myscript.sh: line 122: syntax error: unexpected end of file

What I want is a way to run a command as if it was a cron job, but do it in my shell.

As a side note: does anyone know what would be differnt under cron? (the script already has a #!/bin/sh line)


To answer my own question: I added this to my crontab:

*  *   * * *    bcs     for ((i=$(date +\%M); i==$(date +\%M) ;)) ; do find ~/.crontemp/ -name '*.run' -exec "{}" ";" ; sleep 1; done`

and created this script:

#!/bin/sh
tmp=$(mktemp ~/.crontemp/cron.XXXXX)
mknod $tmp.pipe p
mv $tmp $tmp.pre
echo $* '>' $tmp.pipe '1>&2' >> $tmp.pre
echo rm $tmp.run >> $tmp.pre
chmod 700 $tmp.pre
mv $tmp.pre $tmp.run
cat $tmp.pipe
rm $tmp.pipe

With that, I can run an arbitrary command with a delay of not more than one second.

(And yes, I know there are all kinds of security issue involved in that)

the problem was a fi vs. if problem. Doh!

+1  A: 

You may have a "%" in your crontab. You must escape it (with "\") or it is changed in a newline character.

Sagara
Good point. It's not the % but if cron is parsing the line after that point its probably an escaping problem.
Joshua
+4  A: 

When a script works interactively and fails in cron it's almost always a PATH problem. The default PATH in a cron job process is much much shorter than in an interactive session. The typical result is a "not found" error for some system utility you're trying to run that is not on the PATH in cron.

I would guess that some command you're trying to run is not on the path, therefore the file it was supposed to create is empty and the command that's trying to read that file is giving you this error message.

joefis
One way to investigate is to run a cron job containing just "echo $PATH" then create an interactive session with that PATH value, try to run your script there and see what happens.
joefis
+1  A: 

There are a number of things it could be - output will be redirected elsewhere; environment variables will almost certainly be different, etc. On the information you've given, it could be related to a difference between bash and /bin/sh (which on some systems - including Debian/Ubuntu flavors of Linux - are different, and support slightly different syntax). Cron will usually run the command you give to it using /bin/sh.

Try running:

/bin/sh -c '<command>'

where <command> comes from your crontab. (Of course, if that command uses '' quotes you will need to modify it accordingly...)

psmears
Good points, but in my case with the `#!`, the script should always run under `/bin/sh` and that happens to be a symlink to `/bin/bash` anyway... (the error is inside a script file that cron runs so I'm reasonably confident that the error isn't the command line in crontab)
BCS
I was more thinking that the actual line itself in the crontab might have a syntax issue (you don't specify whether the crontab has just the name, or whether any parameters/pipes/redirects etc are in the crontab). But if `/bin/sh` is symlinked to `/bin/bash` that's much less likely. (Though it's still worth a try, as bash does behave differently if it's invoked as `sh`...). And if the crontab just has the path to the script, with no parameters, then it must be something else :-)
psmears
for some bazare reason... `/bin/sh -c` got the error from the command line...
BCS