[SOLVED] how to make an entry in crontab through shell script called by php page
Linux - NewbieThis 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!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.
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.
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Originally Posted by Ankush Seth
@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.
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.
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.