EndyHK
EndyHK

Reputation: 1

How to run my bash script by crontab -e

I want to run my bash script by crontab -e. However, it doesn't work. Can anyone give me some advise.

My script code is

#!/bin/bash
date > abcaa.txt

The scripts name is "script" The scripts is stored in "/home/mint/Documents"

As seen below, the script is not working because the time is not updated. However, another cronjob is working "echo "hi there". Can I know what wrong with my first cronjob. Thanks!

enter image description here

Upvotes: 0

Views: 442

Answers (2)

McGrady
McGrady

Reputation: 11487

  1. From the screenshot,I think you should run chmod +x script ,so that the script has x permissions for user.

  2. Try to use full path date > /home/mint/a.txt

  3. Confirm this by tail -f /var/log/cron

Hope this helps.

Upvotes: 1

Mark Adams
Mark Adams

Reputation: 9

You can see what a bash script is trying to do by appending "-x" to the shebang thusly:

#!/bin/bash -x

To get the date in the file, you have to provide some formatting, e.g.:

 #!/bin/bash 
 date +"%m %d %Y"  > abcaa.txt

This yields a text file with contents:

 01 06 2017

The trick to making it useful is the format. The list of options is extensive. Just checkout "man date" in a console.

Some examples:

 #!/bin/bash 
 #date +"%m %d %Y"  > abcaa.txt
 date +%D
 date +%F
 date +%r
 date '+%D %r'
 date '+%a %b %c'

The bash file above will produce these results:

 01/06/17
 2017-01-06
 08:09:32 PM
 01/06/17 08:09:32 PM
 Fri Jan Fri 06 Jan 2017 08:09:32 PM MST

Best of luck with it.

Mark

Upvotes: 0

Related Questions