Results 1 to 8 of 8

Thread: Why is it a bad idea to edit /var/spool/cron/crontabs?

  1. #1
    Join Date
    Aug 2013
    Beans
    56

    Why is it a bad idea to edit /var/spool/cron/crontabs?

    So basically, what I've been doing is devising a series of scripts to aid in the automated reinstallation of Linux operating systems on computers that are sent to me.

    Part of this is installing a root cronjob that activates a custom script having to do with reverse SSH tunneling.

    I've been trying to make this automated process as prompt-free as possible (because convenience), and as such I would use a "sed" command to input the cronjob, but I've been hearing people say that editing the /var/spool/cron/crontabs/root file will not work, and to always use the crontab -e command to install cronjobs.

    Can someone just briefly explain why this is? I don't understand.

  2. #2
    Join Date
    Mar 2006
    Location
    Arlington VA USA
    Beans
    445
    Distro
    Ubuntu 18.04 Bionic Beaver

    Re: Why is it a bad idea to edit /var/spool/cron/crontabs?

    Mostly it's just advice to protect you from yourself. For example, you'll get a warning if you inadvertently get more than one crontab -e going for the same user, but you won't get that warning using the editor directly. Also, it makes it easier for the distro to update the files during upgrades if it can make assumptions about what's where in the files.

  3. #3
    Join Date
    Feb 2007
    Location
    West Hills CA
    Beans
    10,046
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Why is it a bad idea to edit /var/spool/cron/crontabs?

    The /var/spool/cron files are created as cron tables are edited. It even says in the files to not edit--because they can get overwritten automatically by any cron edit activity. So it is generally bad form to edit an auto-generated file because you can never be sure when it will get changed. These files are controlled by the system. Why can't you put the script that you want to run in root's crontab (/etc/crontab)?

    The purpose for this convoluted method of running cronjobs is so that the jobs will always get executed and there will never be a file lock collision if the crontab gets edited at the same time it is supposed to execute. It helps to guarantee the execution of scheduled jobs. Editing the spool though does not guarantee execution.
    Last edited by tgalati4; September 11th, 2013 at 08:16 PM.
    -------------------------------------
    Oooh Shiny: PopularPages

    Unumquodque potest reparantur. Patientia sit virtus.

  4. #4
    Join Date
    Aug 2013
    Beans
    56

    Re: Why is it a bad idea to edit /var/spool/cron/crontabs?

    Quote Originally Posted by tgalati4 View Post
    The /var/spool/cron files are created as cron tables are edited. It even says in the files to not edit--because they can get overwritten automatically by any cron edit activity. So it is generally bad form to edit an auto-generated file because you can never be sure when it will get changed. These files are controlled by the system. Why can't you put the script that you want to run in root's crontab (/etc/crontab)?
    I can put that in there, and currently do. But I figured it would be simpler if I could just "sed" the string into a cron-related file as part of an automated process, instead of having to c/p the string into crontab manually.

  5. #5
    Join Date
    Feb 2007
    Location
    West Hills CA
    Beans
    10,046
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Why is it a bad idea to edit /var/spool/cron/crontabs?

    I'm not sure if this is relevant, but there may be an issue with using sed to edit the crontab:

    DEBIAN SPECIFIC
    The "out-of-the-box" behaviour for crontab -l is to display the three line "DO NOT EDIT THIS FILE" header that is placed at the beginning of the
    crontab when it is installed. The problem is that it makes the sequence

    crontab -l | crontab -

    non-idempotent -- you keep adding copies of the header. This causes pain to scripts that use sed to edit a crontab. Therefore, the default behaviour
    of the -l option has been changed to not output such header. You may obtain the original behaviour by setting the environment variable
    CRONTAB_NOHEADER to ’N’, which will cause the crontab -l command to emit the extraneous header.
    -------------------------------------
    Oooh Shiny: PopularPages

    Unumquodque potest reparantur. Patientia sit virtus.

  6. #6
    Join Date
    Aug 2013
    Beans
    56

    Re: Why is it a bad idea to edit /var/spool/cron/crontabs?

    I would use "sed" and insert the string by specifying the line of /var/spool/cron/crontabs/root that I would want to insert it onto. The line would always be available as this would only be performed on a fresh install. So the string here would be:

    sudo sed -i -e '29s:.*:0 0 * * * /home/username/.scripts/script.sh:' /var/spool/cron/crontabs/root

    Or I think that's correct syntax-wise, anyway.

  7. #7
    Join Date
    Feb 2007
    Location
    West Hills CA
    Beans
    10,046
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Why is it a bad idea to edit /var/spool/cron/crontabs?

    As long as the user never runs sudo crontab -e you should be OK, because as soon as a user runs that, the /var/spool will change and it will not include your script.
    -------------------------------------
    Oooh Shiny: PopularPages

    Unumquodque potest reparantur. Patientia sit virtus.

  8. #8
    Join Date
    Aug 2013
    Beans
    56

    Re: Why is it a bad idea to edit /var/spool/cron/crontabs?

    While nobody will have root access except myself and two other coworkers, I will weigh the likelihood of it that we might need to install something else via cronjob. Thanks for your help.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •