I use Ubuntu 16.04 and I login to my remote machine as the user root
, then running the following script that needs to add some scheduled tasks to Unix cron:
#!/bin/bash
su USERNAME
crontab -l 2>/dev/null
cat <<- 'EOF'
* * * * * cron-task1...
* * * * * cron-task1...
* * * * * cron-task2...
* * * * * cron-task2...
* * * * * cron-task3...
* * * * * cron-task3...
EOF
exit
For some reason this code doesn't work, i.e, after execution, the cron tasks won't be printed to the cron from the heredoc, yet the task codes are fine as if I copy them manually like in crontab -e
, they work just fine.
What might be wrong with the above code? Thank you,
Update for Chev_603:
If I execute:
su USERNAME
sudo cat <<- 'EOF' > cron.jobs
* * * * * CRON TASKS...
EOF
I get:
bash: cron.jobs: Permission denied
If I would use a file I would just change ownership, group and permissions as in sudo chmod +x /home/user/Location/Of/Script
but given it's a heredoc, I guess it's not the case.