Where is user's cron job stored after crontab -e?
It is stored in the directory:
/var/spool/cron/crontabs
Containing one file per user.
From man crontab
(at least on my Ubuntu 13):
There is one file for each user's crontab under the
/var/spool/cron/crontabs directory. Users are not allowed to edit the
files under that directory directly to ensure that only users allowed
by the system to run periodic tasks can add them, and only
syntactically correct crontabs will be written there. This is
enforced by having the directory writable only by the crontab group
and configuring crontab command with the setgid bid set for that
specific group.
cronjob entry in crontab -e vs /etc/crontab . Which one is better?
The difference is that the crontab
command is the interface provided by the system for users to manipulate their crontabs. The /etc/crontab
file is a special case file used to implement a system-wide crontab. /var/spool/cron/crontabs/$USER
(or whatever the path happens to be) is an implementation detail.
If you can schedule jobs using the crontab
command, you should do so.
Manually editing the contents of /etc/crontab
(a) requires root access, and (b) is more error-prone. You can mess up your system that way.
If the jobs are to be run under your own user account, there's no need to use root access.
Even if the jobs are to run as root
, it probably still makes more sense to use the crontab
command invoked from the root
account. (For one thing, it should detect syntax errors in the file.)
Personally, I don't use crontab -e
. Instead, I have a crontab file that I keep in a source control system, and I use the crontab filename
form of the command to install it. That way, if I mess something up, it's easy to revert to an earlier version.
does $crontab -e eventually updates /etc/crontab?
crontab -e
and crontab -l
are to edit and display (respectively) the current user's crontab file (which are physically located in /var/spool/cron/crontabs
). Therefore, each user can have their own separate crontab file in that directory. So when you ran crontab -e
and added a cron line, you ran crontab -l
as the same user presumably, and therefore saw the line you added.
/etc/crontab
is a completely different file. You are correct, it is systemwide -- notice that the cron lines in that file specify a user. The same is true for files in /etc/cron.d
, the cron lines in the files will specify a user.
Oh and also, the .d
suffix in cron.d
does not refer to daemon. Check this post out.
How do I create a crontab through a script
Cron jobs usually are stored in a per-user file under /var/spool/cron
The simplest thing for you to do is probably just create a text file with the job configured, then copy it to the cron spool folder and make sure it has the right permissions (600).
Search for a cronjob with crontab -l
/var/spool/cron/crontabs
is the usual parent directory for crontab files. There are files there that have names of users - root is the root crontab, for example. There is a potential for every user on the system to have used crontab -e and created his/her own crontab.
As root you can :
cd /var/spool/cron/crontabs
grep 'search string' *
This command (as root) will tell you what user's crontab has the string. And if it exists.
You would do this if if you are not sure what crontabs things are in. crontab -l
only gives the stuff in YOUR crontab, the user who is currently logged in. If you are sure that is the best place to check:
crontab -l | grep -q 'search string' && echo 'entry exists' || echo 'entry does not exist'
Saving Crontab files
Normally, you use crontab
to create the file, and it stores it in the correct place for your machine.
$ crontab < $HOME/etc/crontab
Then you use crontab -l
to list it.
$ crontab -l
# @(#)$Id: crontab,v 4.2 2007/09/17 02:41:00 jleffler Exp $
# Crontab file for Home Directory for Jonathan Leffler (JL)
#-----------------------------------------------------------------------------
#Min Hour Day Month Weekday Command
#-----------------------------------------------------------------------------
0 * * * * /usr/bin/ksh /work4/jleffler/bin/Cron/hourly
1 1 * * * /usr/bin/ksh /work4/jleffler/bin/Cron/daily
23 1 * * 1-5 /usr/bin/ksh /work4/jleffler/bin/Cron/weekday
2 3 * * 0 /usr/bin/ksh /work4/jleffler/bin/Cron/weekly
21 3 1 * * /usr/bin/ksh /work4/jleffler/bin/Cron/monthly
$
Where I keep my original is immaterial (but it is under source control in $HOME/etc
as it happens). The system has its own copy of the file where it needs it.
If you try placing the files manually, you will get bitten. It might work, but then again, it might not (and it might change in the future). I wouldn't play the risky game of using other than the kosher command to store crontab files for use by crontab
.
Difference between Cron and Crontab?
cron is the general name for the service that runs scheduled actions. crond is the name of the daemon that runs in the background and reads crontab files. A crontab is a file containing jobs in the format
minute hour day-of-month month day-of-week command
crontabs are normally stored by the system in /var/spool/<username>/crontab
. These files are not meant to be edited directly. You can use the crontab
command to invoke a text editor (what you have defined for the EDITOR env variable) to modify a crontab file.
There are various implementations of cron. Commonly there will be per-user crontab files (accessed with the command crontab -e
) as well as system crontabs in /etc/cron.daily
, /etc/cron.hourly
, etc.
In your first example you are scheduling a job via a crontab. In your second example you're using the at
command to queue a job for later execution.
Related Topics
Different Behavior of Which Command in Zsh and Bash
How to Add a Custom System Call on X86 Ubuntu Linux
How to Get Pyinstaller to Working on Ubuntu
Installing New Version of Python on Debian Linux Server
How to Make Linux Ignore a Keyboard While Keeping It Available for My Program to Read
Understanding The Netstat Output
Sendmail/Procmail - Get Mail Sender and Mail Subject, Utf8 Encoding Issues
How to Select a Static Port Number for a Custom App
Raw Socket Access as Normal User on Linux 2.4
Can an Rpm Spec File "Include" Other Files
Error Hh604: Error Running JSON-Rpc Server: Error:0308010C:Digital Envelope Routines::Unsupported
How to Disable Greeting Message When Ssh in to a Server
Cmake, Rpath, $Origin and @Loader_Path
How to Type "Cargo Run" Without Needing to Set The Ld_Library_Path Shell Variable