questions on crontab - cron

I have following questions on cron.
What is the preferred way for automating cron jobs? Using a) crontab command-line options (eg. crontab -e) or b) editing /etc/crontab. What is the difference between the two? It's not exactly clear to me.
Is crontab user specific? If I am logged in as a a user say "anup", and add jobs using crontab -e, will the job be user-specific? However, in some of the cron examples I checked online, username is provided as a field between the time string and the command to be executed.
Can the 'Mail to user" option be controlled for each job? As in, for job 1: MailTO: root for job 2: MailTO: anup.

The crontab utility is a commandline tool that maintains the crontab files for individual users.
crontab -l lists/views the crontab for the current user
crontab -e edits the crontab
You can type man crontab for full documentation on most linux/mac systems.
The crontab is specific to the user. If you're logged in as anup, then crontab -e will edit anup's crontab file. You can specify the user with the -u flag (crontab -u)
I'm not 100% sure, but I believe that you can specify a new MAILTO= setting and it will take effect for all processes executed afterwards.

Related

Delete/clear active cron job

How can I delete or clear all the cron job that I made previously and just run new cron job that I assigned? I'm using crontab -r but it just clear in the crontab display, but it still runs that cron job and the previous cron job that I have already deleted by using that code.
After I clear cron job using crontab -r, I run crontab -l and it shows this output.
No crontab for trygcp
Use this command,
crontab -e
to access crontab, in there you can deleted specific line or job you created.
About the output you are getting, that only means there is no crontab created under the username trygcp
What you can do is this:
crontab -u [username] -e
Where:
-u define user
-e edit user's crontab
This command will create a crontab under your username, but remember you must have root privilege for you to do this.

UNIX : Editing system crontab (/etc/crontab) and restarting cron services after edit

I want to edit system cron tab (/etc/crontab). I read that crontab -e is the best way to edit crontab and you need not restart cron services if you edit this way. However I am not able to edit /etc/crontab using crontab -e (this command edits the crontab associated with the user, not system crontab). So is there any better way of editing /etc/crontab (other than using VI editor- which I am doing now). Do I need to restart cron services if I edit /etc/crontab using VI edior?
There are two ways of cronjobs, one is by editing /etc/crontab and sending a SIGHUP the cron daemon. The other way is to use crontab -e to edit
a crontab entry, which is done for the current user or the one mentioned with -u. The -u option can only be used by root. The crontabs
created this way can be found in the directory
/var/spool/cron/crontabs/
and are named after the user with which uid the jobs will be started. In this case you don't need to SIGHUP cron, a normal user can't do this anyway.
Note: The syntax is slightly different to /etc/crontab: You can't enter an other user name to execute the cronjob.
You could do something like this
echo "0 23 * * * yum -y update > /dev/null 2>&1" >> /var/spool/cron/root
Then verify with
crontab -l

How do I make sure a cron job will run?

I've always used cpanel to set up con jobs but I don't have cpanel now.
So I added a PHP file in the cron.hourly but I want to be sure it will run.
There must be some way to do this. Like a command that lists all the cron jobs that exist?
I am on Debian 7 64 bit.
If you use crontab -e the crontab job will be syntax-checked
If you are just editing /etc/crontab or /etc/cron.*/* there is no automatic checking
nothing will check the code of your job will actually run successfully.
To list all cron jobs for a user:
crontab -u <user> -l
To list all cron jobs for root:
crontab -l

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

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
What is the risk of does not using crontab -e?
Are there a risk that my edit will not taken account in the cron
schedule?
Should I restart cron with /etc/init.d/cron restart?
The main risk to not using crontab to edit or replace cronfiles is that directly writing to the cronfile (including using sed to do so) does not lock the file, and therefore two simultaneous edits could have unpredictable and even disastrous consequences.
You don't have to use crontab -e; the following forms of editing are semi-safe, in the sense that they won't randomly combine two edits. (But they are still unsafe; an edit could be overwritten):
To add lines:
{ crontab -u user -l; echo "$this"; echo "$this_too"; } | crontab -u user -
To delete lines or do more complicated edits:
crontab -u user -l | sed "$my_wonderful_sed_script" | crontab -u user -
In any case, you don't need to restart cron. It will notice the changed modification time of the cronfile.
When editing the crontab file using your regular editor, the schedule will not be updated. The system will still do the scheduled tasks as before the edit. This will drive the administrator mad.
When cron restarts, such as a reboot or a restart of the process, the current crontab files will be read and processes are scheduled as described in the current present files.
I remember sending a 'kill -HUP' or 'kill -15' to the PID of the cron process to force the process flushing the cache and reading the crontab files again.

crontab entry for individual user

On my RHEL5 box, I have so far set up cron jobs by placing entries in the /etc/crontab file which is for safely reasons, only editable by root.
Are there other ways to set up cron jobs for individual users? Preferably, I would like each user to have their own cron file that they can edit at will without requiring root privileges.
Can this be done?
Users can create and edit their own crontabs with crontab -e. They can view their crontab with crontab -l. They can remove their crontab with crontab -r.
man -s1 crontab for more information.
crontab entry is user specific only. a user can make/schedule its own jobs in crontab or you can say that all files on which user is having access
http://www.weblogitech.com/2015/12/what-is-cron-tab-in-linux-and-unix.html

Resources