简体   繁体   中英

Running shell script as a cron on Ubuntu

I've written a simple script date.sh in /home/user/test

date -u > file.txt

This should write the current date and time in file.txt. I've given it the necessary access right with chmod +x date.sh . When I run the script from the terminal it works fine and updates the time in the text file.

But when I run it as a cron

* * * * * /bin/sh /home/user/test/date.sh

I don't see the dates changing in file.txt As my knowledge about this is limited at the time, my question is, how can I run the date.sh script as a cronjob

Try providing a full output file path in the script.

#!/bin/sh
date -u > /home/user/test/file.txt

The problem is with date.sh, it should NOT be date > file.txt All the files should contain their entire path. In this case the script should be

date > /home/user/test/file.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM