2

Is there a way to schedule a task using crontab by root user but should not be visible using crontab command i.e, crontab -l either for root user or normal users?

  • 2
    Please edit your question and tell us what user should the cron job be run as and what user shouldn't be able to see it. – terdon Feb 18 '16 at 13:04
5

If you want to schedule a task using cron, an alternative to crontab in many distributions is to add a file to /etc/cron.d, in the traditional system crontab format (the variant which specifies the user). Tasks defined in this way do not show up in crontab -l's output.

For example, on Debian, amavisd-new's Spamassassin maintenance is scheduled by /etc/cron.d/amavisd-new, which contains

#
#  SpamAssassin maintenance for amavisd-new
#
# m h dom mon dow user  command
18 */3  * * *   amavis  test -e /usr/sbin/amavisd-new-cronjob && /usr/sbin/amavisd-new-cronjob sa-sync
24 1  * * *   amavis  test -e /usr/sbin/amavisd-new-cronjob && /usr/sbin/amavisd-new-cronjob sa-clean
  • For Example: I have created file with the name hidcron in the location "/etc/cron.d/" with following line added to that file: * * * * * /sbin/ifconfig >> /root/file1. But the given task is not working – Karthik Vee Feb 22 '16 at 13:56
  • @KarthikVee as mentioned in the answer, you need to specify the user in the task definition, so you'd end up with * * * * * root /sbin/ifconfig >> /root/file1. – Stephen Kitt Feb 22 '16 at 13:59
2

If your cron job is setup for root user, other users won't be able to see it with crontab -l unless they connect as root.

Maybe you can tell us more about what you want to achieve by doing this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.