Why I've got no crontab entry on OS X when using vim?

前端 未结 12 1752
执念已碎
执念已碎 2020-12-07 07:41

I would like to use cron on my Mac. I choose it over launchd, because I want to be able to use my new knowledge on Linux as well. However, I cannot seem to get

相关标签:
12条回答
  • 2020-12-07 07:56

    Just follow these steps:

    1. In Terminal: crontab -e.
    2. Press i to go into vim's insert mode.
    3. Type your cron job, for example:

      30 * * * * /usr/bin/curl --silent --compressed http://example.com/crawlink.php
      
    4. Press Esc to exit vim's insert mode.

    5. Type ZZ to exit vim (must be capital letters).
    6. You should see the following message: crontab: installing new crontab. You can verify the crontab file by using crontab -l.

    Note however that this might not work depending on the content of your ~/.vimrc file.

    0 讨论(0)
  • 2020-12-07 08:00

    I did 2 things to solve this problem.

    1. I touched the crontab file, described in this link coderwall.com/p/ry9jwg (Thanks @Andy).
    2. Used Emacs instead of my default vim: EDITOR=emacs crontab -e (I have no idea why vim does not work)

    crontab -lnow prints the cronjobs. Now I only need to figure out why the cronjobs are still not running ;-)

    0 讨论(0)
  • 2020-12-07 08:02

    Use another text editor

    env EDITOR=nano crontab -e 
    

    or

    env EDITOR=code crontab -e 
    
    0 讨论(0)
  • 2020-12-07 08:09

    Difference between cron and launchd

    As has been mentioned cron is deprecated (but supported), and launchd is recommended for OS X.

    This is taken from developer.apple.com

    Effects of Sleeping and Powering Off

    If the system is turned off or asleep, cron jobs do not execute; they will not run until the next designated time occurs.

    If you schedule a launchd job by setting the StartCalendarInterval key and the computer is asleep when the job should have run, your job will run when the computer wakes up. However, if the machine is off when the job should have run, the job does not execute until the next designated time occurs.

    All other launchd jobs are skipped when the computer is turned off or asleep; they will not run until the next designated time occurs.

    Consequently, if the computer is always off at the job’s scheduled time, both cron jobs and launchd jobs never run. For example, if you always turn your computer off at night, a job scheduled to run at 1 A.M. will never be run.

    0 讨论(0)
  • 2020-12-07 08:09

    As the previous posts didn't work for me because of some permissions issues, I found that creating a separate crontab file and adding it to the user's crontab with the -u parameter while root worked for me.

    sudo crontab -u {USERNAME} ~/{PATH_TO_CRONTAB_FILE}
    
    0 讨论(0)
  • 2020-12-07 08:10

    The use of cron on OS X is discouraged. launchd is used instead. Try man launchctl to get started. You have to create special XML files that define your jobs and put them in a special place with certain permissions.

    You'll usually just need to figure out launchctl load

    http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/launchctl.1.html

    http://nb.nathanamy.org/2012/07/schedule-jobs-using-launchd/

    Edit

    If you really do want to use cron on OS X, check out this answer: https://superuser.com/a/243944/2449

    0 讨论(0)
提交回复
热议问题