1st430n avatar

What to do when a shell command doesn't work in crontab

firstaeon

Published: 06 Aug 2018 › Updated: 06 Aug 2018What to do when a shell command doesn't work in crontab

What to do when a shell command doesn't work in crontab

Linux-aeon.png

So you have a nice command which should run every 5 minutes.
You correctly configure your crontab with the help of the super-simple , crontab.guru.

You are sure about it, you test it, it works.

Ok so I'm gonna leave it running for the night, every 5 minutes and tomorrow morning results will be there.

You wake up and....

It did not run!

But it works if I run it on the terminal! - What the heck?

The workaround

1 - Put it in a script

A very good workaround is to place whatever command you want to run in a dedicated script.

*/5 * * * * /home/user/archiver.sh

Content of script:

cp /home/user/results.csv /home/user/results_archive/$(date +%Y%m%d%H%M)_results.csv

Remember to give it executable permissions:

chmod +x /home/user/archiver.sh 

Otherwise crontab (and you and anybody else) is not gonna be
able to execute it!*

2 - What if it still does not work?

In this case it should work by exporting your PATH inside the script.

First get the contents of the PATH variable for your user by running the command

env

In the command's output you will have a line called

PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:....
....

Copy the whole line and place it in your script at the top.

Content of script:

PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:....
cp /home/user/results.csv /home/user/results_archive/$(date +%Y%m%d%H%M)_results.csv

Thank you! Stay tuned for more Linux tips, tricks and much more!

resh aquasummit.png

Leave What to do when a shell command doesn't work in crontab to:

Written by

crypto, mining, linux, music, guitar, tech, IT, security, finance, investing, traveling, pets and everything else tickling my brain

Read more #linux posts


Best Posts From 1st430n

We have not curated any of firstaeon's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From 1st430n