LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 10-01-2013, 04:40 AM   #1
Ankush Seth
LQ Newbie
 
Registered: Sep 2013
Posts: 20

Rep: Reputation: Disabled
how to make an entry in crontab through shell script called by php page


Hello all
I want to schedule jobs in ubuntu and for that i am using crontab.
it is giving me what i want when i make an entry to it by going through cmd line with the help of crontab -e.

Now i want a shell script which can do the same task for me.

Now the twist here is that i am calling this script through php's shell_exec() method. Now nothing is happening i am writing in shell script :
"
(crontab -l; echo "$query")|crontab -
"

please help and teach me the way to do it as i am new to shell programming and linux.
I will be thankful to you!
 
Old 10-01-2013, 06:50 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,527

Rep: Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437
Probably you would need to start a shell script (from cron) and you only need to modify that shell script.
 
Old 10-01-2013, 07:39 AM   #3
Ankush Seth
LQ Newbie
 
Registered: Sep 2013
Posts: 20

Original Poster
Rep: Reputation: Disabled
thanks for the reply but i am starting a script from php function and i am not able to make an entry in cron via that shell script
 
Old 10-01-2013, 11:19 AM   #4
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,131

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
I've never tried modifying a crontab in this manner, but the first thought that pops into my head (and I didn't test it) is that the "crontal -l" part might be outputting something extraneous that is causing problems. You might try replacing "crontab -l" with "crontab -l 2>/dev/null". Also, if you have * in the crontab entries, as you probably do, those may be getting grabbed by the shell and expanded somehow.

Or maybe don't use a subshell and try something like this instead:

echo `crontab -l 2>/dev/null` 'The new cron entry you want to add' | crontab -

Note the use of backticks around the command and the use of single quotes around the new entry. You want to make sure the shell is not grabbing some special character and expanding it. I don't know if my example above covers all those bases (without testing it), but you get the general idea of what needs to be done.

Since you mentioned PHP, you might be doing this from a webpage. Just be aware of the inherant dangers of modifying system level stuff from a webpage if random users are accessing that webpage. Procede with adequate caution.

Last edited by haertig; 10-01-2013 at 11:22 AM.
 
1 members found this post helpful.
Old 10-01-2013, 01:44 PM   #5
YankeePride13
Member
 
Registered: Aug 2012
Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7
Posts: 262

Rep: Reputation: 54
Might be a path issue. Try putting the path to the crontab program. For example, in Ubuntu 12.04 it would be :
Code:
/usr/bin/crontab -l
 
Old 10-01-2013, 05:28 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
It depends on what you are really trying to do.

the crontab command only has access to the user that apache is running under - thus you cannot change user crontab entries...

Second, depending on the system, it won't work as the Apache user id is normally restricted from running just anything (that is why the cgi directory restrictions are there). SELinux (if on red hat) will block you as well.

Remember, different web logins are not different user identities - they are strictly interpreted by Apache for access to apache controlled resources. So giving apache the ability to change user identities and put jobs in user logins will open up your system to a complete takeover.
 
1 members found this post helpful.
Old 10-03-2013, 01:23 AM   #7
Ankush Seth
LQ Newbie
 
Registered: Sep 2013
Posts: 20

Original Poster
Rep: Reputation: Disabled
@haertig - i have done what as sou said but the result is again same. i am unable to make an entry in crontab. Kindly help!
 
Old 10-03-2013, 01:29 AM   #8
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,202

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
Quote:
Originally Posted by Ankush Seth View Post
@haertig - i have done what as sou said but the result is again same. i am unable to make an entry in crontab. Kindly help!
just asking, but did you read what jpollard said? this is something that is by design impossible as allowing such would be a security hole big enough to pilot an aircraft carrier though with room to spare.

that being said you could perhaps have the php script upload the crontab entry to an apache writable dirctory and write a shell script that then picks up the uploaded crontab entry and apply it to the real crontab.

note though even that is quite dangerous.
 
Old 10-03-2013, 01:31 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,527

Rep: Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437Reputation: 2437
Try what I told you: add a crontab entry to invoke a script and modify only that script.
 
2 members found this post helpful.
Old 10-03-2013, 11:09 AM   #10
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,131

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
I hate to ask this question, but I probably should to make sure we are all on the same page.

Do you realize that there are multiple crontabs on a system? So if user "A" creates a crontab entry, then user "B" will not be able to see that entry, because user "B" has their own (different) crontab.

Maybe are you creating the crontab entry via your webserver (user "A") and then trying to review the results as user "B"? You won't see them if this is the case.

This is a pretty basic question, so please take no insult fom it, because I don't know your level of experience.
 
1 members found this post helpful.
Old 10-03-2013, 01:29 PM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
And I have to emphasize haertigs comment about practicing safe hex:

Quote:
Originally Posted by haertig View Post
be aware of the inherant dangers of modifying system level stuff from a webpage if random users are accessing that webpage. Procede with adequate caution.
 
1 members found this post helpful.
Old 10-03-2013, 02:52 PM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,345

Rep: Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910Reputation: 3910
There are ways that you can do this with a bit more safety, but it is a total kludge...not to mention what haertig said about the different users.

If I had to do this (which I WOULD NOT, due to the inherent security issues), I'd take the input from the PHP page, and write it out to a text-file somewhere. I'd then have another, separate script running via cron, that would read that input file, and look at it. Things like the "passwd" command would be rejected, and other obvious, blatant security-type things would also get rejected. If the command is safe, insert it into the crontab FILE, then restart cron when you're done.

As said; NONE of that is a good idea, but it at least separates things and builds in a layer of pseudo-security. I am NOT advocating this in any way...but it is a possible work-around.
 
1 members found this post helpful.
Old 10-03-2013, 06:54 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Also, as hinted above, why not create a crontab owned by the apache user; that should limit the potential damage.
You could also look into phpsuexec (I think its called?)

All that being said, as per unSpawn & TB0ne, try and resist doing it (this way) if at all if possible.
 
1 members found this post helpful.
Old 10-03-2013, 07:10 PM   #14
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 7,681
Blog Entries: 10

Rep: Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080Reputation: 2080
How about a script.php to backup said crontab?
 
Old 10-03-2013, 07:17 PM   #15
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,202

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
now that i've thought about it a bit more, if you really want to manage cron jobs remotely via a web interface, your best bet would probably be webmin.
 
  


Reply


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
[SOLVED] how to run shell script command from php web page Ankush Seth Linux - Newbie 8 09-03-2013 11:35 AM
[SOLVED] script in called by crontab executing but not writing data to file. Dafydd Programming 2 08-02-2012 02:35 AM
testing shell scripts before entry into crontab. bartonski Linux - Server 1 10-15-2009 09:28 PM
make a syslog entry from php script. womd Linux - Newbie 2 07-10-2008 05:56 AM
Crontab entry updation prob in different shell hinetvenkat Solaris / OpenSolaris 3 11-01-2006 10:56 AM


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

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