pythonbashraspberry-pi

Get boolean result to see if crontab exists


In Python or Bash, is there an easy way to receive a boolean result on whether or not the crontab exists?

crontab -e #shows me manually

Solution

  • The crontab -l command displays your crontab. If you have no crontab, it will display no crontab for <username> on stderr and exit with error code 1.

    If you have an empty crontab, it will display no errors and will exit with a status code of 0.

    So if you want to check for empty OR does-not-exist, you can just see if crontab -l produces any output:

    if [ $(crontab -l | wc -c) -eq 0 ]; then
      echo crontab is empty
    fi
    

    If you want to check explicitly for an absent crontab, then

    if ! crontab -l; then
      echo you have no crontab
    fi