0

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.

0

1 Answer 1

1

Despite this being marked as a duplicate, I answered it because the issue was that both the syntax of cat and crontab were off.

There is no existing crontab, and your syntax is off. For example, if I do this with no cron configured, this happens:

$ crontab -l
no crontab for anon

And than you're simply sending the contents of the here doc to stdout.

So first create a crontab file like this:

cat <<- 'EOF' > cron.jobs

   * * * * * cron-task1...
   * * * * * cron-task2...
   * * * * * cron-task3...

EOF

crontab cron.jobs

Than confirm it worked:

crontab -l
 * * * * * cron-task1...
 * * * * * cron-task2...
 * * * * * cron-task3...

The reason why it wasn't working is because crontab -l simply displays the contents of the users cron file to stdout:

From man crontab:

   The -l option causes the current crontab to be displayed on standard output. See the note under DEBIAN SPECIFIC below.

Also, instead of using su , you can use

cat cron.jobs |crontab -u user -

Putting it all together, add this to a script, say cron.sh and try this:

#!/bin/bash
user="$@"
cat <<- 'EOF' > cron.jobs

   * * * * * cron-task1...
   * * * * * cron-task2...
   * * * * * cron-task3...

EOF


cat cron.jobs|crontab -u $user -

And then:

bash cron.sh username

But I think what you were going for was more like:

cat <<< '
* * * * *  cron-task1... ' |crontab -
8
  • I would very much like to do it only with su USERNAME but I get permission denied when doing this with su USERNAME... i.e when I do sudo cat <<- 'EOF' > cron.jobs, I get permission denied.
    – user423047
    Nov 18, 2016 at 14:28
  • Useless use of cat Nov 18, 2016 at 15:22
  • Chev_603, can you please give me your opinion on the permissions issue?
    – user423047
    Nov 19, 2016 at 0:36
  • @DavidFoerster, if I remove cat, the command doesn't work for me...
    – user423047
    Nov 19, 2016 at 0:38
  • Which instance of cat did you remove and what exactly did you substitute? I was thinking of: crontab -u user cron.jobs and crontab - <<< '* * * * * command'. You can even skip the intermediate file for the heredoc. Nov 19, 2016 at 10:21