0

I want to run a job with cron on reboot as a particular user. I have been able to do this successfully using crontab to write to /var/spool/cron/crontabs/username with something like:

 @reboot ./run.sh >>~/tracefile 2>&1

However, I want to use /etc/cron.d/filename. Cron jobs in this file require an extra column to indicate what user runs, so I use:

 @reboot wwwuser ./run.sh >>~/tracefile 2>&1

This doesn't seem to work. Should I be able to use @reboot with a username in a cron.d file?

3
  • What flavor/version of cron? What OS?
    – freiheit
    Jun 12, 2014 at 18:11
  • Ubuntu 14.04, checking cron...
    – fschwiet
    Jun 12, 2014 at 18:14
  • I don't know how to check cron version.
    – fschwiet
    Jun 12, 2014 at 18:17

1 Answer 1

3

I think that instead of adding @reboot wwwuser ... to /var/spool/cron/crontabs/username, you should run crontab -e as user wwwuser and add:

@reboot ./run.sh >>~/tracefile 2>&1

Just in case make sure to use the full path to the script.

In case you're making these changes programmatically, you could try the following:

#write out current crontab
crontab -u wwwuser -l > mycron_wwwuser

#echo new cron into a temp cron file
echo "@reboot ./run.sh >>~/tracefile 2>&1" >> mycron_wwwuser

#install new cron file
crontab -u wwwuser mycron_wwwuser

#remove the temp cron file
rm mycron_wwwuser

... or better this one:

crontab -u wwwuser -l | { cat; echo "@reboot ./run.sh >>~/tracefile 2>&1"; } | crontab -u wwwuser -
4
  • I'm making these changes programmatically, so I don't think I can use the interactive editor (crontab -e).
    – fschwiet
    Jun 12, 2014 at 19:21
  • @fschwiet, I've updated my answer. I hope it helps.
    – coda
    Jun 12, 2014 at 19:34
  • This did help. I did some things wrong when I originally tried to use crontab -u and now I see how to do that correctly.
    – fschwiet
    Jun 12, 2014 at 22:10
  • 1
    I still wonder if I can etc/cron.d, I read someplace that /var/spool/cron data (where crontab writes to) can be lost (in particular, when upgrading cron).
    – fschwiet
    Jun 12, 2014 at 22:11

Not the answer you're looking for? Browse other questions tagged .