William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

CentOS: run crontab as root

CentOS: run crontab as root

Sometimes in a cron job, we have to write security directory such as /var/data/mysql/, in this case we need root permission.
There are several ways to run cron job by using root role. A simple solution is using ‘sudo’:
Normally as a normal user ‘demo’, I use sudo to borrow root permission by adding entry in /etc/sudoers with ‘nopasswd:all’. Similarly, in this case, we need to set sudo option in /etc/sudoers.
1. $ sudo vi /etc/sudoers:

Defaults    requiretty
Defaults:demo !requiretty

After the setting, we can sudo directly in crontab, without extra-declaration:
2. $ crontab -e

*/10 * * * * sudo /usr/bin/indexer -c $HOME/etc/file.conf --rotate delta --quiet

indexer will update /var/data/mysql directory every 10 minutes by using root permission. This works fine.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: