Crontab not working

Questions about applications and software
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
rdh61
Level 4
Level 4
Posts: 208
Joined: Sun Apr 26, 2009 1:06 pm

Crontab not working

Post by rdh61 »

Hi,

I cannot get crontab to run my script. Cognisant of the possibility that my script might be wrongly constructed, as a test I added this dummy job to crontab:

Code: Select all

* * * * * env > /home/[myusername]/env.output
as suggested here: https://askubuntu.com/questions/23009/w ... ot-working.

No output is produced.

I checked that crontab is running (pgrep cron) and that timezone is updated (sudo service cron restart).

Help greatly appreciated.

Many thanks.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
SimonPeter
Level 5
Level 5
Posts: 579
Joined: Tue Jul 13, 2021 5:13 am

Re: Crontab not working

Post by SimonPeter »

rdh61 wrote: Tue Dec 07, 2021 12:41 pm ...
Try /usr/bin/env instead (this one can be run without a shell):

Code: Select all

* * * * * /usr/bin/env > /home/[myusername]/env.output
To run the shell when the command is not run in the shell, use /bin/sh -c '<your_command>' (for POSIX sh scripts) or /bin/bash -c '<your_command>' (for BASH scripts).
rdh61
Level 4
Level 4
Posts: 208
Joined: Sun Apr 26, 2009 1:06 pm

Re: Crontab not working

Post by rdh61 »

SimonPeter wrote: Tue Dec 07, 2021 1:59 pm
Try /usr/bin/env instead (this one can be run without a shell):

Code: Select all

* * * * * /usr/bin/env > /home/[myusername]/env.output
To run the shell when the command is not run in the shell, use /bin/sh -c '<your_command>' (for POSIX sh scripts) or /bin/bash -c '<your_command>' (for BASH scripts).
Thank you, that worked, but I am afraid I have no idea what it means to run a command "in the shell" or to "run the shell when the command is not run in the shell".

Please could you tell me then what is wrong with the real script I want to run? It is this:

Code: Select all

0 8 * * * /opt/FreeFileSync/FreeFileSync '/home/robert/BatchRun.ffs_batch'
Many thanks.
SimonPeter
Level 5
Level 5
Posts: 579
Joined: Tue Jul 13, 2021 5:13 am

Re: Crontab not working

Post by SimonPeter »

rdh61 wrote: Tue Dec 07, 2021 5:53 pm
SimonPeter wrote: Tue Dec 07, 2021 1:59 pm ...
Thank you, that worked, but I am afraid I have no idea what it means to run a command "in the shell" or to "run the shell when the command is not run in the shell".

Please could you tell me then what is wrong with the real script I want to run? It is this:

Code: Select all

0 8 * * * /opt/FreeFileSync/FreeFileSync '/home/robert/BatchRun.ffs_batch'
Many thanks.
I think the line should be amended to

Code: Select all

0 8 * * * /bin/sh -c 'echo "FreeFileSync started on $(date)" > /home/robert/FreeFileSync_last_started_on.txt ; /opt/FreeFileSync/FreeFileSync "/home/robert/BatchRun.ffs_batch"'
This also creates a text file named FreeFileSync_last_started_on.txt in your home directory.
Before adding to crontab, please verify this works by running it in a Terminal.

"Without a shell" means that the program is run using its executable (binary / script) + arguments only (not using a shell).
eg. system calls from the exec() family in C/C++, QProcess::start() etc.,

"With a shell" means to run a command in the shell ( /bin/sh for most POSIX systems).
eg. system() call in C/C++, the Terminal app (ie. x-terminal-emulator), the TTY shell etc.,
RIH
Level 9
Level 9
Posts: 2908
Joined: Sat Aug 22, 2015 3:47 am

Re: Crontab not working

Post by RIH »

The format to get FreeFileSync work in cron is...


env DISPLAY=:0.0 Full path to FreeFileSync program FreeFileSync Full path to the FreeFileSync batch job backup.ffs_batch
Where the pieces in red are user submitted.

I just cracked it... :D
The secret is the Display bit..
Image
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Crontab not working

Post by AndyMH »

You might have a look at unison instead of freefilesync. unison is native linux whereas freefilesync is ported from win. I run it in a script at startup to sync important files to my NAS, I also use notify-send so the script sends me a notification telling me what it has done.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
rdh61
Level 4
Level 4
Posts: 208
Joined: Sun Apr 26, 2009 1:06 pm

Re: Crontab not working

Post by rdh61 »

AndyMH wrote: Wed Dec 08, 2021 6:19 am You might have a look at unison instead of freefilesync. unison is native linux whereas freefilesync is ported from win. I run it in a script at startup to sync important files to my NAS, I also use notify-send so the script sends me a notification telling me what it has done.
Would you be so kind as to post the script here?
rdh61
Level 4
Level 4
Posts: 208
Joined: Sun Apr 26, 2009 1:06 pm

Re: Crontab not working

Post by rdh61 »

SimonPeter wrote: Wed Dec 08, 2021 3:33 am
I think the line should be amended to

Code: Select all

0 8 * * * /bin/sh -c 'echo "FreeFileSync started on $(date)" > /home/robert/FreeFileSync_last_started_on.txt ; /opt/FreeFileSync/FreeFileSync "/home/robert/BatchRun.ffs_batch"'
This also creates a text file named FreeFileSync_last_started_on.txt in your home directory.
Before adding to crontab, please verify this works by running it in a Terminal.
This works. Thank you very much. I also changed the script to * * * * * to facilitate testing.

UPDATE: No, it doesn't work, it sends the last_started file, but it is not actually syncing my files.
Last edited by rdh61 on Wed Dec 08, 2021 8:07 am, edited 1 time in total.
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Crontab not working

Post by AndyMH »

rdh61 wrote: Wed Dec 08, 2021 7:39 am Would you be so kind as to post the script here?
Have PMed you.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
rdh61
Level 4
Level 4
Posts: 208
Joined: Sun Apr 26, 2009 1:06 pm

Re: Crontab not working

Post by rdh61 »

AndyMH wrote: Wed Dec 08, 2021 8:06 am
rdh61 wrote: Wed Dec 08, 2021 7:39 am Would you be so kind as to post the script here?
Have PMed you.
Thank you!
rdh61
Level 4
Level 4
Posts: 208
Joined: Sun Apr 26, 2009 1:06 pm

Re: Crontab not working

Post by rdh61 »

RIH wrote: Wed Dec 08, 2021 5:49 am The format to get FreeFileSync work in cron is...


env DISPLAY=:0.0 Full path to FreeFileSync program FreeFileSync Full path to the FreeFileSync batch job backup.ffs_batch
Where the pieces in red are user submitted.

I just cracked it... :D
The secret is the Display bit..
Thanks for your response.

As is, this does not work for me. When I run this command in the terminal I am told that there is no file FreeFileSync. If I delete that part of the command, I am told there is no file backup.ffs_batch. If I then delete that part of the command it works, that is:

env DISPLAY=:0.0 Full-path-to-FreeFileSync-program Full-path-to-the-FreeFileSync-batch-job

So that's good. I would also like it to log when it has run in a file in my home folder. Do you know how that can be achieved?

Thanks again.
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Crontab not working

Post by AndyMH »

Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
RIH
Level 9
Level 9
Posts: 2908
Joined: Sat Aug 22, 2015 3:47 am

Re: Crontab not working

Post by RIH »

As is, this does not work for me.
Then I don't believe that you have entered your paths correctly.

Code: Select all

env DISPLAY=:0.0 /home/bob/Programs/FreeFileSync/FreeFileSync /home/bob/Programs/FreeFileSync/backup.ffs_batch
Works fine for me both in Terminal & as a cron job..
Image
rdh61
Level 4
Level 4
Posts: 208
Joined: Sun Apr 26, 2009 1:06 pm

Re: Crontab not working

Post by rdh61 »

Thank you!
rdh61
Level 4
Level 4
Posts: 208
Joined: Sun Apr 26, 2009 1:06 pm

Re: Crontab not working

Post by rdh61 »

RIH wrote: Wed Dec 08, 2021 8:42 pm
As is, this does not work for me.
Then I don't believe that you have entered your paths correctly.

Code: Select all

env DISPLAY=:0.0 /home/bob/Programs/FreeFileSync/FreeFileSync /home/bob/Programs/FreeFileSync/backup.ffs_batch
Works fine for me both in Terminal & as a cron job..
Yes, thank you. I figured that was it. I had misinterpreted your original message.
Locked

Return to “Software & Applications”