[SOLVED] Crontab not executing shell when date is added
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I created a shell which work well when I run it from the command line:
# ./test.sh
I added a time stamp to record the execution like this:
# ./test.sh > log`date +%Y%m%d-%H%M%S`
This also works well. Every line executed inside the script is recorded. No problem here...
However, when I want to schedule this in crontab, it does not work if I add the log part at the back with the date. I only works if I do this in crontab:
* * * * * ./test.sh
Or
* * * * * ./test.sh > log
But crontab does not even execute the command if I do this:
* * * * * ./test.sh > log`date +%Y%m%d-%H%M%S`
...where the date part is added at the back of the file.
Do you guys know if there is a limitation in crontab for not running certain commands?
I'd put that inside the shell script TBH, it'd be nicer in there. If not, use the full path to data, and replace the `backticks` with $(brackets) as that's much nicer.
Hi Acid_Kewpie, Thanks! Are you saying that I should create a second script, just to launch the first script? For instance, in crontab I will have an entry like:
* * * * * ./run.sh
... to start the first script and inside the run.sh, I put:
No, I mean you should put the logging inside test.sh. But you don't have to. I just find cron a hell of a lot easier if you keep the commands simple. Do check what I said about the path though.
I see, yes I agree. Keep the command simple in cron is the best way. I'm not sure hove to add the logging inside the test.sh? How will I be able to record each step from within the script itself? Can you shed some light on this?
Hi, yes I tried the date suggestion, but it did not work. Unfortunatly I cannot provide the script. There is confidential info inside the script like keys and other company related stuff which would not be ideal to share over the net. I tried executing the scripts from within another script as mentioned in my first reply and that works well. The log file is created perfectly with the timestamp. Thanks for you help.
The problem is that the % sign has a special meaning in crontab. From man 5 crontab:
Quote:
Percent-signs (%) in the command, unless escaped with backslash (\),
will be changed into newline characters, and all data after the first %
will be sent to the command as standard input.
As suggested by the man page, the solution is to escape the percent signs in the date format, e.g.
Hi Colucix, This is exctly what I wanted. I added your suggestion into cron and it is creating the log files perfectly. Thanks for the help. I will mark this thread as SOLVED. Cheers guys!
You don't *need* it, but if it does error, you'll most likely be emailed the errors, which can be messy and unwanted. you set that off every minute and eventually you have a 500gb root mail file killing your system.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.