doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Unable to run executable .sh file from crontab?

Thu Aug 08, 2019 10:55 am

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
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

User avatar
B.Goode
Posts: 11172
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

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

Thu Aug 08, 2019 11:00 am

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.

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

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

Thu Aug 08, 2019 11:04 am

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?
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

User avatar
dickon
Posts: 1886
Joined: Sun Dec 09, 2012 3:54 pm
Location: Home, just outside Reading

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

Thu Aug 08, 2019 11:15 am

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.
As it is apparently board policy to disallow any criticism of anything, as it appears to criticise something is to criticise all the users of that something, I will no longer be commenting in threads which are not directly relevant to my uses of the Pi.

User avatar
B.Goode
Posts: 11172
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

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

Thu Aug 08, 2019 11:17 am

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.

jbudd
Posts: 1642
Joined: Mon Dec 16, 2013 10:23 am

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

Thu Aug 08, 2019 11:18 am

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.

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

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

Thu Aug 08, 2019 11:40 am

Thanks all, will give it a shot now.
WD
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

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

Thu Aug 08, 2019 12:07 pm

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................................................
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

doubleudee1
Posts: 164
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

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

Thu Aug 08, 2019 3:13 pm

All working fine now, thanks for your help everyone.
WD
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

Fenlig
Posts: 17
Joined: Tue May 29, 2012 4:38 am

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

Thu Aug 08, 2019 6:58 pm

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

Return to “General discussion”