Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 12-04-2012, 11:45 PM   #1
Registered: Dec 2011
Posts: 91
Blog Entries: 1

Rep: Reputation: Disabled
Editing crontab using script

Hi All,

So i wanted to add edit cron entry through script and one more thing is that cron is on different host and script is on different one

say ...

i have written script on localhost

through script want to edit crontab of globalhost

no issue with ssh i can connect them with each other..

Thanks in advance!
Old 12-05-2012, 12:15 AM   #2
Senior Member
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Which disto you're using? And are you doing it using any user account or root?
First take backup of /etc/crontab. Then you can (it's sample only) try:
echo "*/15 * * * * <command>" >> /etc/crontab
If it work then same way can be added to the script. But let's know what you've tried so far..

Last edited by shivaa; 12-05-2012 at 12:16 AM.
Old 12-05-2012, 01:42 AM   #3
Registered: Dec 2011
Posts: 91

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Redhat Tikanga 5.6

I have not done anything yet ...

and what about ssh so is should be like

echo "*/15 * * * * <command>" >> username@globalhost:/etc/crontab?

like above i should run or other way...?
Old 12-05-2012, 06:47 AM   #4
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590Reputation: 3590
Originally Posted by azheruddin View Post
echo "*/15 * * * * <command>" >> username@globalhost:/etc/crontab?
Unprivileged users luckily aren't allowed to mess with the system-wide crontab. Ensure that what you want to add is free of errors and if it really should be run as root and not from an unprivileged user account. Example (dunno about crontab exit value) for an unprivileged user account:
_TMPFILE=`mktemp -p ${HOME} .crontab.XXXXXXXXXX` && {
crontab -l > "${_TMPFILE}"; cp "${_TMPFILE}" "${_TMPFILE}.bk"
echo "*/15 * * * * <command>" >> "${_TMPFILE}"
crontab "${_TMPFILE}" || crontab "${_TMPFILE}.bk"
rm -f "${_TMPFILE}" "${_TMPFILE}.bk"
If you need to run as root you could for example try
ssh username@globalhost
echo -en 'cat << EOF >> /etc/crontab\necho */15 * * * * <command> \nEOF\n' > /tmp/cmd
sudo /bin/sh /tmp/cmd
to avoid any Sudo-related "requiretty" errors. (And never mind there's about a gazillion things wrong with allowing any unprivileged user to execute things as root with non-root input.)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Crontab not editing in solaris 8. crown_bd Solaris / OpenSolaris 6 09-06-2007 02:23 AM
editing crontab meniscus Linux - Newbie 5 12-11-2006 10:33 AM
Crontab editing chobin SUSE / openSUSE 12 06-01-2006 11:12 AM
editing crontab mfran2002 Linux - Software 2 02-20-2006 08:09 AM
editing crontab djgerbavore Slackware 12 05-30-2005 10:24 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:17 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration