DSM-G600, DNS-3xx and NSA-220 Hack Forum

Unfortunately no one can be told what fun_plug is - you have to see it for yourself.

You are not logged in.

Announcement

#1 2011-01-01 19:28:27

bound4h
Member
Registered: 2010-04-12
Posts: 209

Shell script syntax

Why is that when this script runs as a cronjob (nightly at 3am), I get this in the error log (transmission WAS running):

wc: No such file or directory
test: 1: unknown operand
[Sat Jan 1 03:30:01 GMT 2011] Transmission not running...not executing unpacker script.

But when I run it now (with transmission running), as root, I don't get any errors, but it says Transmission is NOT runnning:

[Sat Jan 1 12:22:18 GMT 2011] Transmission not running...not executing unpacker script.

I've also ran it with "su nobody -c /ffp/var/scripts/unpacker.sh" and I get the same thing:

[Sat Jan 1 12:22:51 GMT 2011] Transmission not running...not executing unpacker script.

What is wrong with my script?

#!/bin/sh
##############################################################################
# ---[ unpacker.sh ] ---
# script to look for rar files inside a specific directory.
# if found, unrar them
# w 10/29/08 horto
##############################################################################
DLDIR=/mnt/HD_a2/Torrents/completed
LOG=/mnt/usb/logs/unpack.log
UNRAR=/ffp/bin/unrar

if test `ps | grep transmission | wc -l` -gt 1 ; then

echo [`date`] Transmission running...executing unpacker script. >> $LOG

# quick and dirty check to make sure no active downloads.
# because we don't want to unrar/delete files that may be actively downloading!
if [ `/ffp/bin/transmission-remote -l | wc -l` -gt 2 ]; then
        echo [`date`] Quiting, a download is active... >> $LOG
  # stop, because a download is active.
  exit 0
 
else
echo [`date`] No active downloads...continuing... >> $LOG
  # continue; because there are no active downloads.
  # check incoming DLDIR
  for FILE in `find $DLDIR -name "*.rar"`; do
    if [ "$FILE" != "*.rar" ]; then
      # shell-fu to extract to path containing the rar
      FILENAME=`expr //$FILE : '.*/\(.*\)'`
      UNPACKDIR=`echo $FILE | sed -e s/$FILENAME//g`
      echo [`date`] Extracting "$FILE" ... >> $LOG
     
      # unrar file to the directory its sitting in
      $UNRAR x -y "$FILE" "$UNPACKDIR" >> /dev/null 2>&1
      echo [`date`] ... done extracting. >> $LOG
     
      # cleanup - remove the rar file(s)
      # note: match .rar, .r01, .r02 ... etc
      echo [`date`] Removing "$FILENAME and rar files." >> $LOG
      for j in `find $UNPACKDIR -name "*.r??"`; do
        rm $j
      done
    fi
  done
  echo [`date`] Done unpacking/extracting \(if any necessary\)... >> $LOG

fi

else

echo [`date`] Transmission not running...not executing unpacker script. >> $LOG

fi

exit 0

When I run "ps | grep transmission | wc -l" from the command line, I get 6.

Offline

 

#2 2011-01-01 20:02:46

rsd76
Member
Registered: 2010-04-18
Posts: 47

Re: Shell script syntax

Hi,

This has all to do with the fact that running a script from the crontab, the "user" environment isn't the same as a user which is logged on via a shell.
AFAIK, when you logon using ssh to the nas, your shell reads /ffp/etc/profile and sets all kinds of enviroment variables.

Cron jobs don't. The grep in your script is probably /bin/grep and not /ffp/bin/grep.

The easiest option is to add one line at the top of the script:
PATH=/ffp/bin:$PATH

Then all command in /ffp/bin are accessible using only the command (even unrar in your script).

I do not know if your SHEBANG (#!/bin/sh) might also give problems.
You could write: #!/ffp/bin/sh and use the funplug shell.

You could also use "grep -c transmission" instead of "grep transmission | wc -l"

I see you use greater than 1 in your condition. This is due to that "grep transmission" is shown in the "ps -ef" output.
You could write: "if test `ps | grep -c [t]ransmission` - gt 0 ; then".
This grep searches for transmission in the "ps" output. But the grep itself is shown as "grep [t]ransmission"
If for some reason transmission is only running 1 process and grep isn't in the "ps" output, your script will not run.

OK. A lot of info. But the extra line: PATH=/ffp/bin:$PATH should work.


Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us. (Calvin & Hobbes)

Offline

 

#3 2011-01-02 03:02:24

bound4h
Member
Registered: 2010-04-12
Posts: 209

Re: Shell script syntax

isn't the line you said to add already in there as:

export PATH=/ffp/sbin:/ffp/bin:$PATH

Should I take out 'export'?

Offline

 

#4 2011-01-02 20:14:04

rsd76
Member
Registered: 2010-04-18
Posts: 47

Re: Shell script syntax

Yes, the export line is already in the /ffp/etc/profile script.
But scripts running from the cron do not read this script. This script is only read if you "physically" log on.
That is why you should add the "export PATH=/ffp/sbin:/ffp/bin:$PATH" to your upacker.sh script.
At least I do not see the PATH variable anywhere in your script.

It should just work when your script also has:

PATH=/ffp/bin:$PATH

in it.


Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us. (Calvin & Hobbes)

Offline

 

#5 2011-01-03 06:18:47

bgravato
Member
Registered: 2010-11-04
Posts: 44

Re: Shell script syntax

There are a few things in your script that might fail... After a quick look I think the most problematic line could be this:

Code:

if test `ps | grep transmission | wc -l` -gt 1 ; then

ps without any option will return only commands running from the current shell, meaning if you type ps you'll probably only get 2 processes running: bash and ps

For it to work you should add some options to show all processes running (for example: ps -A).

I'd change the line above to something like:

Code:

if [ "`ps -A |grep transmission`" != "" ]; then

Like it has been already said, not having a path set might get you into trouble too if you don't call the commands by their full path name.

If you want to load your prefs from profile you can just simply call profile from the script with:

Code:

. /ffp/etc/profile

The syntax is "dot space file".

Regarding export... if you want to define a variable just to be used withtin the script where it is declared you don't need to "export" it, so you can simply type "VAR = value".

Last edited by bgravato (2011-01-04 05:16:47)

Offline

 

#6 2011-01-04 06:29:42

bound4h
Member
Registered: 2010-04-12
Posts: 209

Re: Shell script syntax

Thanks, I have two scripts.  One is for watching a torrent folder and one for unpacking the downloaded torrents with unrar.  Looks like the torrent script had the 'export PATH...' in it already but unpacker.sh did not (as you said).  Have since fixed and everything runs fine.  Thanks guys

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2010 PunBB