Cron Job Not Starting shell script

I created a script from an article on this site that uses ffmpeg in a batch file. It works great if I run it manually but it won’t start in a cron job. I also put ffmpeg in my $PATH but that didn’t help. I’m not allowed to post a link to the article. Any ideas?
Thanks
Jim

Hi jimkoh,
welcome to our forum.
In cron make sure any mentioned script is specified with an absolute path. Also have you checked your script is executable?

Regards.

My script is executable and I’e put in the full path and it still doesn’t work. If I run it as a one time scheduled command it works. It also works if I type it manually. It just doesn’t work as a cron job.

Hi Jimkoh,

Does your script get to be executed properly by cron? If you put something like

echo “test” > /tmp/test.out

Into the script, will the “test” string appear in the target text file?

If so, this could be an environment issue. You can try executing your .bashrc or .bash_profile (depending on Distro) as one of the first commands in your script, it sets most of the environmental variables that are available when you run the script by hand, or schedule it with at.

Does it appear the cron job in the below folder?
/var/spool/cron/crontabs/

For example if It should run as root it is in the file
/var/spool/cron/crontabs/root

You can also try to restart the cron daemon.

Yes it does. It also says to not edit this file, but the command looks o.k.

Can u paste the output of this command?

crontab -l

Thanks.

* * * * * ffmpeg-batch mkv mkv /mnt/tv2/temp/ /mnt/tv2/finished/ '-c:a copy -c:v libx265'
I have also tried it this way
* * * * * ffmpeg-batch.sh mkv mkv /mnt/tv2/temp/ /mnt/tv2/finished/ '-c:a copy -c:v libx265'
And like this
* * * * * /mnt/data/ffmpeg-batch mkv mkv /mnt/tv2/temp/ /mnt/tv2/finished/ '-c:a copy -c:v libx265'

You can try to specify the full path of the script ffmpeg-batch.sh itself otherwise you have to insert in the crontab, BEFORE any of these commands:
PATH = $PATH:/path/to/script

Have you declared interpreter within your ffmpeg-batch script? Perhaps you can try something like:
* * * * * /bin/bash /mnt/data/ffmpeg-batch mkv mkv /mnt/tv2/temp/ /mnt/tv2/finished/ '-c:a copy -c:v libx265'

Furthermore, be aware that there could be multiple issues why your script does execute using cron. When you are executing a job via cron you are in a sense creating a new minimalist /bin/sh shell session which is different from the one you normally use when you open up a terminal. From this reason do not assume that cron execution has a correct PATH to your executable binaries, files and the script itself.

At this stage you need to develop some sort of bottom up troubleshooting approach. Here is just an example:

Create a basic shell script which does nothing else other than creating eg. TEST file within /tmp/ directory.

#!/bin/bash
/usr/bin/touch /tmp/TEST

Implement the script execution as a cron job and see whether it executes. If it does you should see TEST file created within/tmp/ directory. In case all good keep adding more code into the script until you hit a problem.

If you do not see the TEST file created within/tmp/ directory investigate what happened as the issue is most likely with your cron job declaration or cron itself. Speaking of which, this sounds obvious but are you sure that cron daemon is running on your system? I had that issue before :slight_smile:

Another trick you can try is to redirect the script’s STDOUT and STDERR into a file by using &> redirection. This will help you troubleshoot as why the script does not execute. For example:

* * * * * /bin/bash /mnt/data/ffmpeg-batch mkv mkv /mnt/tv2/temp/ /mnt/tv2/finished/ '-c:a copy -c:v libx265' &> /tmp/out

Then, after cron’s supposed script execution check whether the /tmp/out exists and see its content. It should contain all script output including errors which occurred during the execution.

Hope this helps

Lubos

Thank you for your help. I finally got it to work by putting ~ in front of / and putting the script in it’s own folder. Now I’m having another issue since getting cron working, but I don’t know if it’s a cron or script issue. This is the script I’m using:

#! /bin/bash

srcExt=$1
destExt=$2

srcDir=$3
destDir=$4

opts=$5

for filename in "$srcDir"/*.$srcExt; do

        basePath=${filename%.*}
        baseName=${basePath##*/}

        ffmpeg -i "$filename" $opts "$destDir"/"$baseName"."$destExt"

done

echo "Conversion from ${srcExt} to ${destExt} complete!"

The issue now is if cron checks my folder for new videos before the current one is finished, then it starts a new process for each file which really slows conversion. So if I have 3 files, it starts 3 jobs. But if I run the script in terminal it processes the videos one at a time in succession like it should.

Hi Jimkoh,

This is normal, your cron schedule starts the script every minute, but the script does not know it should not start the processing of the source video files, because another instance of the script is already running. You could set the cron schedule to start the script less frequently, but that reduces the responsiveness of the whole thing.

You can add the functionality to the script to exit if another instance of it is already running however; a common solution is the lockfile mechanism.

Upon start, your script can test for the existence of a simple empty file, called lockfile. If it does not exist, the script creates it, then start processing the data. If the lockfile exist, the script can exit without doing anything else. On successful completion the script can remove the file, so on next execution it can run the processing part again.
While the script is processing, the lockfile will be present, and any other instances of the script that start in the meantime will exit without entering the processing part, or deleting the lockfile.

This mechanism is a common implementation of “the job should start as soon as possible, but one and only one processing thread may be working at a given time”. It is used widely in situations where multiple workers would cause consistency.

A post was split to a new topic: Canon printer offline