22

I add some job in crontab file on a server.

  • When I log out and the server is still on, will the job still run?

  • Does it matter if I create a screen or tmux session and run some shell in it and detach it before log out?

Tim
  • 101,790

1 Answers1

34

cron is a process which deals with scheduled tasks whether you are logged in or not. It is not necessary to have a screen or tmux session running since the cron daemon will execute the scheduled tasks in separate shells.

See man cron and man crontab for details.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Lambert
  • 12,680
  • when I log out, which user will run and own the job scheduled in crontab? If it is not me, can that cause some potential problem? – Tim Apr 21 '15 at 13:08
  • 1
    That depends on which crontab you edited. If you edit the crontab of a user the entries are executed as that user. If you edited the /etc/crontab file the user is often specified between the interval and the command like: "17 * * * * root cd / && run-parts --report /etc/cron.hourly". Besides it does also depend on the platform you are using. Ubuntu uses a different cron than for instance Solaris – Lambert Apr 21 '15 at 13:14
  • I use ubuntu. What is "the crontab of a user"? In "the crontab of a user", can I also specify which user will run a scheduled job? – Tim Apr 21 '15 at 13:26
  • Ubuntu cron has some explanation text in the crontab files. As a user perform crontab -e to read about the user crontab file and cat /etc/crontab to read about the server-wide crontab file. From a user crontab file you can not run commands using a different user although it is possible to use sudo if passwordless commands are specified – Lambert Apr 21 '15 at 13:33
  • Thanks. /etc/crontab is for system-wide, then where is the crontab file for a user? – Tim Apr 21 '15 at 19:42
  • 2
    According to man crontab on Ubuntu the user crontab file is located in /var/spool/cron/crontabs. But be carefull, the manual also say that those files are not intended for direct editing. You should use crontab -e from the user context or crontab -e -u username from the root user to edit the user crontab. – Lambert Apr 21 '15 at 19:49
  • Thanks. I just got a reply which says we can make a user's crontab file to be ~/.crontab by crontab ~/.crontab. Is it a good suggestion? – Tim Apr 21 '15 at 19:53
  • While reading the referenced reply the main purpose is that you have a (backup) copy of the crontab. If backups are arranged properly you should not have to do this by yourself. – Lambert Apr 21 '15 at 20:02
  • 1
    it should be noted that user crontabs often require that the home folder of that user to be mounted to do useful work. this can be a problem if the users have encrypted home folders decrypted at login (e.g. with ecryptfs). encrypted homes are less common in server scenarios, granted. – init_js Sep 16 '16 at 00:58
  • macOS: I beg to differ – ospider Dec 12 '21 at 00:19
  • @Tim: I believe you misunderstood the reply: ~/.crontab is not a special file and not your actual crontab. It's merely a local file you may keep for backup purposes, and you can name it whatever you want, say ~/crontab.txt. You can save it to your crontab using crontab ~/crontab.txt and generate it with crontab -l > ~/crontab.txt – MestreLion May 10 '22 at 15:04