Execute crontab as user Joe using php...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chr1s

    #1

    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

  • Erwin Moller

    #2
    Re: Execute crontab as user Joe using php...

    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/yoursheduledscr ipt.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

    Comment

    • Erwin Moller

      #3
      Re: Execute crontab as user Joe using php...

      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/yoursheduledscr ipt.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

      Comment

      • Chr1s

        #4
        Re: Execute crontab as user Joe using php...

        On Mar 23, 2:50 pm, Erwin Moller
        <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
        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/yoursheduledscr ipt.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

        Comment

        • Jerry Stuckle

          #5
          Re: Execute crontab as user Joe using php...

          Chr1s wrote:
          On Mar 23, 2:50 pm, Erwin Moller
          <since_humans_r ead_this_I_am_s pammed_too_m... @spamyourself.c omwrote:
          >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/yoursheduledscr ipt.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.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          Working...