Page 1 of 1

Unable to run executable .sh file from crontab?

Posted: Thu Aug 08, 2019 10:55 am
by doubleudee1
Hi
I have writen the code below and would like to automate it so I have aded a line to crontab.

Code: Select all

## this file is being ran from the directory on the HDD '/CCTV/RearOfHouse/' (which is connected to the pi)

# create an array with all the files/dir inside ~/CCTV/RearOfHouse/
#!/bin/bash
arr=(~/CCTV/RearOfHouse/*.mp4)

# iterate through array using a counter
for ((i=0; i<${#arr[@]}; i++)); do
################### SET UP VARIABLES FOR CODE ###########################
   j=${arr[$i]:30:6}"_mp4" ## 201903_mp4

   k=${arr[$i]:0:27}"mp4Files/"              ## /home/pi/CCTV/RearOfHouse/mp4Files/

   h=${arr[$i]:0:48}                                   ## /home/pi/CCTV/RearOfHouse/ARC20190315080521.mp4

   m=${arr[$i]:0:48}                                 ##/home/pi/CCTV/RearOfHouse/ARC20190315080521.mp4
   
   n=${k}${j}                                               ## /home/pi/CCTV/RearOfHouse/201903

  o=${arr[$i]:30:6}                                     ## 201903

oo="${k}${o}""_mp4"
}
   l="${k}${j}"
}
###################  CREATE ANY NEW DIRECTORIES THAT NEED CREATING ##############
 mkdir -p "${oo}"                                         ### this creates new directories in location of this exec file

################# MOVE ALL FILES INTO RELEVANT DIRECTORIES NEXT ##################
### mv pathtofileA pathtofileB ###
aa="201901_mp4"
bb="201902_mp4"
cc="201903_mp4"
dd="201904_mp4"
ee="201905_mp4"
ff="201906_mp4"
gg="201907_mp4"
hh="201908_mp4"
ii="201909_mp4"
jj="201910_mp4"
kk="201911_mp4"
ll="201912_mp4"

if [ ${j} = ${aa} ]; then
     mv "${m}" "${l}"
fi
 if [ ${j} = ${bb} ]; then
     mv "${m}" "${l}"
fi
 if [ ${j} = ${cc} ]; then
    mv "${m}" "${l}"
fi
 if [ ${j} = ${dd} ]; then
    mv "${m}" "${l}"
 fi
if [ ${j} = ${ee} ]; then
     mv "${m}" "${l}"
fi
 if [ ${j} = ${ff} ]; then
     mv "${m}" "${l}"
fi
 if [ ${j} = ${gg} ]; then
    mv "${m}" "${l}"
fi
 if [ ${j} = ${hh} ]; then
    mv "${m}" "${l}"
fi
 if [ ${j} = ${ii} ]; then
     mv "${m}" "${l}"
fi
 if [ ${j} = ${jj} ]; then
     mv "${m}" "${l}"
fi
 if [ ${j} = ${kk} ]; then
    mv "${m}" "${l}"
fi
 if [ ${j} = ${ll} ]; then
    mv "${m}" "${l}"
 fi

done

################# ABOVE OUTPUTS HAVE CREATED DIRS AND PUT FILES IN THEM ################ 
I have saved the file as Createmp4Dirs.sh and made it executable.

If I run the command :-
pi@raspberrypi:~/CCTV/FrontOfHouse $ ./Createmp4Dirs.sh
Everything runs fine, directories are created and the relevant files are put into them.

I have added the the following line to my crontab file:-
20 * * * * source /home/pi/CCTV/FrontOfHouse/ .CreatejpgDirs.sh

but it does not run this line?
I log in as pi, and have created the crontab from the 'pi@raspberrypi:~' using 'crontab -e'

Can anyone tell me why it is not running and how to fix it please, as my files build up in the base directories very fast and searching for a particular file becomes problematic.

Thanks

Re: Unable to run executable .sh file from crontab?

Posted: Thu Aug 08, 2019 11:00 am
by B.Goode
One problem - maybe not the only one - is that arr=(~/CCTV/RearOfHouse/*.mp4) will not take you to the place where the files are stored if you run it as the 'root' user during startup.

Re: Unable to run executable .sh file from crontab?

Posted: Thu Aug 08, 2019 11:04 am
by doubleudee1
Apologies, I have 2 CCTV cams, 'FrontOfHouse' and 'RearOfHouse' where the files are sent from, I may have mixed up Front, for Rear above, but there are two identical scripts with minor changes to suit either.
A file may come in as '/home/pi/CCTV/RearOfHouse/ARC20190315080521.mp4', so how would the array not be pointed to that? would I have to put the full path into the array?

Re: Unable to run executable .sh file from crontab?

Posted: Thu Aug 08, 2019 11:15 am
by dickon
When you try to run this from cron, the daemon doesn't know what to do with it, because it isn't actually an executable shell script. Move the

Code: Select all

#!/bin/bash
to the top and it should execute correctly.

It works when you run it manually, because your login shell will execute it, in the absence of any other interpreter. The #! line must be first for the kernel to run it, though.

Re: Unable to run executable .sh file from crontab?

Posted: Thu Aug 08, 2019 11:17 am
by B.Goode
A file may come in as '/home/pi/CCTV/RearOfHouse/ARC20190315080521.mp4', so how would the array not be pointed to that? would I have to put the full path into the array?
Because the '~' shortcut takes you to the Home Directory for the current user. During startup that is the root user, not your 'everyday' pi user.

So Yes, you need the full absolute path.

Re: Unable to run executable .sh file from crontab?

Posted: Thu Aug 08, 2019 11:18 am
by jbudd
Your crontab line 20 * * * * source /home/pi/CCTV/FrontOfHouse/ .CreatejpgDirs.sh has an error - the space before .CreatejpgDirs.sh
Edit - the space and the dot are both wrong. 20 * * * * /home/pi/CCTV/FrontOfHouse/CreatejpgDirs.sh

And why are you calling the script with source? If the file is executable then it can be called directly.

The tilde in ~/CCTV/RearOfHouse/*.mp4 is probably better replaced with /home/pi to avoid any possibility of misdirection, as B.Goode suggests.

Re: Unable to run executable .sh file from crontab?

Posted: Thu Aug 08, 2019 11:40 am
by doubleudee1
Thanks all, will give it a shot now.
WD

Re: Unable to run executable .sh file from crontab?

Posted: Thu Aug 08, 2019 12:07 pm
by doubleudee1
OK, done all above, #!/bin/bash at top of all files, arr=(/home/pi/CCTV/FrontOfHouse/*.jpg) is now the array
and crontab changed as below

15 * * * * /home/pi/CCTV/RearOfHouse/CreatejpgDirs.sh

20 * * * * /home/pi/CCTV/FrontOfHouse/CreatejpgDirs.sh

Waiting to see what happens................................................

Re: Unable to run executable .sh file from crontab?

Posted: Thu Aug 08, 2019 3:13 pm
by doubleudee1
All working fine now, thanks for your help everyone.
WD

Re: Unable to run executable .sh file from crontab?

Posted: Thu Aug 08, 2019 6:58 pm
by Fenlig
Also if you had problems in future would outputting to a logfile help?

Code: Select all

20 * * * * /home/pi/CCTV/FrontOfHouse/CreatejpgDirs.sh >> /home/pi/CCTV/output.log