469,900 Members | 1,635 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,900 developers. It's quick & easy.

Execute crontab as user Joe using php...

How do I execute a crontab command using php (not CLI) as user Joe?

I don't have any problem getting output from commands such as 'ls'
using passthru and exec but I am stumped with crontab.

Many thanks!

Chris

Mar 23 '07 #1
4 3714
Chr1s wrote:
How do I execute a crontab command using php (not CLI) as user Joe?

I don't have any problem getting output from commands such as 'ls'
using passthru and exec but I am stumped with crontab.

Many thanks!

Chris
Hi Chris,

1) user Joe:
If you want user Joe to execute the cronjob, login as use Joe and edit his
cronjob by typing:
crontab -e

Now the contab program opens Joe's cronjobs.
Most systems open in VI editor, since I hate that thing because I am a nOOb,
I prefer opening in pico: a small editor that works like a human brain,
unlike VI (shoot me if you want. :P).

If you want pico (or nano) too, type first:
export EDITOR=pico
then give the crontab -e command.

2) shedule
add a line like this (all on 1 line):
0 1 * * * cd /home/yourdir/public_html/ && php -q
/home/yourdir/public_html/yoursheduledscript.php

to let the script run each night at 1 o'clock.

I am not sure about the cd, but had trouble using a script without cd-ing to
the dir first.

Hope that helps.

Good luck.

Regards,
Erwin Moller
Mar 23 '07 #2
Erwin Moller wrote:
Chr1s wrote:
>How do I execute a crontab command using php (not CLI) as user Joe?

I don't have any problem getting output from commands such as 'ls'
using passthru and exec but I am stumped with crontab.

Many thanks!

Chris

Hi Chris,

1) user Joe:
If you want user Joe to execute the cronjob, login as use Joe and edit his
cronjob by typing:
crontab -e

Now the contab program opens Joe's cronjobs.
Most systems open in VI editor, since I hate that thing because I am a
nOOb, I prefer opening in pico: a small editor that works like a human
brain, unlike VI (shoot me if you want. :P).

If you want pico (or nano) too, type first:
export EDITOR=pico
then give the crontab -e command.

2) shedule
add a line like this (all on 1 line):
0 1 * * * cd /home/yourdir/public_html/ && php -q
/home/yourdir/public_html/yoursheduledscript.php

to let the script run each night at 1 o'clock.

I am not sure about the cd, but had trouble using a script without cd-ing
to the dir first.

Hope that helps.

Good luck.

Regards,
Erwin Moller

One addition: when hunting for problems think of the cronjob as:
1) login as user Joe
2) Joe gives the commands by hand.

So if your script isn't doing what you want, you can always try to find what
is wrong by doing excactly the same thing being Joe.

Good luck.

Regards,
Erwin Moller
Mar 23 '07 #3
On Mar 23, 2:50 pm, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
Erwin Moller wrote:
Chr1s wrote:
How do I execute a crontab command using php (not CLI) as user Joe?
I don't have any problem getting output from commands such as 'ls'
using passthru and exec but I am stumped with crontab.
Many thanks!
Chris
Hi Chris,
1) user Joe:
If you want user Joe to execute the cronjob, login as use Joe and edit his
cronjob by typing:
crontab -e
Now the contab program opens Joe's cronjobs.
Most systems open in VI editor, since I hate that thing because I am a
nOOb, I prefer opening in pico: a small editor that works like a human
brain, unlike VI (shoot me if you want. :P).
If you want pico (or nano) too, type first:
export EDITOR=pico
then give the crontab -e command.
2) shedule
add a line like this (all on 1 line):
0 1 * * * cd /home/yourdir/public_html/ && php -q
/home/yourdir/public_html/yoursheduledscript.php
to let the script run each night at 1 o'clock.
I am not sure about the cd, but had trouble using a script without cd-ing
to the dir first.
Hope that helps.
Good luck.
Regards,
Erwin Moller

One addition: when hunting for problems think of the cronjob as:
1) login as user Joe
2) Joe gives the commands by hand.

So if your script isn't doing what you want, you can always try to find what
is wrong by doing excactly the same thing being Joe.

Good luck.

Regards,
Erwin Moller- Hide quoted text -

- Show quoted text -
Apologies, I should have been more specific.

I want to append to a user's crontab using a php script...not run a
script from crontab.

For example,

exec('crontab MyFile', $var);

I don't know how to adapt this code to work for user 'Joe'?

Thanks,

Chris

Mar 23 '07 #4
Chr1s wrote:
On Mar 23, 2:50 pm, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
>Erwin Moller wrote:
>>Chr1s wrote:
How do I execute a crontab command using php (not CLI) as user Joe?
I don't have any problem getting output from commands such as 'ls'
using passthru and exec but I am stumped with crontab.
Many thanks!
Chris
Hi Chris,
1) user Joe:
If you want user Joe to execute the cronjob, login as use Joe and edit his
cronjob by typing:
crontab -e
Now the contab program opens Joe's cronjobs.
Most systems open in VI editor, since I hate that thing because I am a
nOOb, I prefer opening in pico: a small editor that works like a human
brain, unlike VI (shoot me if you want. :P).
If you want pico (or nano) too, type first:
export EDITOR=pico
then give the crontab -e command.
2) shedule
add a line like this (all on 1 line):
0 1 * * * cd /home/yourdir/public_html/ && php -q
/home/yourdir/public_html/yoursheduledscript.php
to let the script run each night at 1 o'clock.
I am not sure about the cd, but had trouble using a script without cd-ing
to the dir first.
Hope that helps.
Good luck.
Regards,
Erwin Moller
One addition: when hunting for problems think of the cronjob as:
1) login as user Joe
2) Joe gives the commands by hand.

So if your script isn't doing what you want, you can always try to find what
is wrong by doing excactly the same thing being Joe.

Good luck.

Regards,
Erwin Moller- Hide quoted text -

- Show quoted text -

Apologies, I should have been more specific.

I want to append to a user's crontab using a php script...not run a
script from crontab.

For example,

exec('crontab MyFile', $var);

I don't know how to adapt this code to work for user 'Joe'?

Thanks,

Chris
Chris,

Then you're going to have to su to user Joe or otherwise have permission
to open/read/write Joe's crontab. You can do it with posix_seteuid() or
posix_setuid() - but these are dangerous - you have to enable the change
SUID bit for PHP which potentially allows any PHP script to set any
userid, including root. Not very safe.

A better way would be to execute a shell script which has permission to
change the uid and add to the crontab. This limits your exposure.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Arkascha | last post: by
4 posts views Thread by smarty | last post: by
3 posts views Thread by Frank R. Suchy | last post: by
3 posts views Thread by rbt | last post: by
2 posts views Thread by David Garamond | last post: by
3 posts views Thread by Sandman | last post: by
5 posts views Thread by roger.moon2 | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.