Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Is there a way I can include the command that was executed in the output when I'm logging to a file? For example:

* * * * * date && /path/to/php /path/to/script.php >> cron.log 2>&1

Gives me:

Wed Oct  2 14:56:50 PDT 2013
output from script

How can I include the executed command too, to make the log look like this:

Wed Oct  2 14:56:50 PDT 2013
/path/to/php /path/to/script.php
output from script

I'm looking to add this in the crontab and not in the script.

share|improve this question
3  
Why not something like * * * * * bash -c 'set -x; date && ... ' >> cron.log 2>&1? –  Zoredache Oct 2 '13 at 22:05
3  
@Zoredache, excellent idea, you should provide that as an answer. You can add the -x option to the bash command: bash -xc 'date && ...' –  glenn jackman Oct 3 '13 at 0:41
1  
Does someone want points for this? :) –  Steve Robbins Oct 8 '13 at 19:17

1 Answer 1

Shamelessly putting together the excellent points of @Zoredache and @Glenn in comments, you can wrap your cron command inside bash -xc like this:

* * * * * bash -xc 'date && ... ' >> cron.log 2>&1
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.