linux - What is the risk when editing crontab file without the "crontab -e" command?

07
2014-07
  • Mohamed KALLEL

    I developed a script in which I add lines to the crontab file with echo command and I remove lines with sed command.

    I do not know the risk of that especially that I find in some web site that we have to edit crontab file with

    crontab -e
    
    1. What is the risk of does not using crontab -e?
    2. Are there a risk that my edit will not taken account in the cron schedule?
    3. Should I restart cron with /etc/init.d/cron restart?
  • Answers
  • nohup
      • Crontab would fail from the point where there is syntax error (linux)
      • Crontab would fail if there is an accidental new line is added (solaris)
    1. You don't need to restart cron.
  • jjlin
    1. Syntax or other errors may not be detected.
    2. Yes. Different cron implementations have different ways of detecting crontab changes. For example, Vixie cron relies on its crontab command to update the modification time of the spool directory to signal a change. You would have to know how your cron implementation works.
    3. It's best to restart cron if you're not sure exactly how your cron works.

    Bottom line: You should use crontab if possible. It's there for a reason. Your crontab command may have an option to take an existing crontab as an argument and install it to the system-wide crontab directory, so you could make whatever edits you need to using sed and then use crontab to install your changes.


  • Related Question

    linux - ImageMagick's "import" command line program failing to take a screenshot from CRON script
  • bakytn

    Hello I wrote a simple command line script which is suppose to take my screenshot every 5 minutes.

    (using ImageMagick's "import" program)

    here is the script (shottr.sh):

    #!/bin/sh

    PTH="/home/username/images"
    NM=`date +%j`_`date +"%F %k:%M"`
    
    /usr/bin/import -window root -resize 1024 "$PTH/$NM.png" &
    echo "Screenshot" | festival --tts &
    

    this is working fine if I execute by hand i.e:

    # ./shottr.sh

    or

    #sh shottr.sh

    the cron is set like this:

    */5 * * * * /bin/sh /home/username/scripts/shottr.sh

    the script itself is being executed (I hear a voice saying: "Screenshot") but the actual screenshot is not taken.

    P.S: Be assured that it is NOT permission issue (I placed a simple "touch" invocation and file was created)

    may be if it's run from cron...it doesn't have a "window" so it can't take a screenshot from nowhere?? If that is the case, then how can I workaround it?


  • Related Answers
  • larsks

    When you're running the script from cron it doesn't have access to your DISPLAY environment variable, which is how it knows what X11 display with which to interact. You may be able to get it to work by adding the following to your script:

    # Set display to :0 if it's not already set.
    : ${DISPLAY:=:0}
    export DISPLAY
    

    This assumes that your DISPLAY is always :0, which is true if you're logging in on the console but not true if you're running X remotely.