SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Cannot add script to crontab

    I want to schedule a php script to run under the root user via the crontab, but the crontab doesn't seem to be working correctly.

    I log into ssh as root and enter 'crontab -e'.
    A blank file opens in nano, and at the top of the screen it reads:
    File: /tmp/crontab.XXXX7tYrwv

    This is obviously not my root crontab, and I cannot seem to add any new jobs.

    Does anyone have an idea of what the problem might be or how to fix it?


    (Note: The last 6 characters of the filename are different (random) each time.)

  2. #2
    SitePoint Guru silver trophy JamesColin's Avatar
    Join Date
    May 2009
    Location
    Jomtien, Pattaya, Thailand
    Posts
    904
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could it be because of your host config? Are you sure this is the way to do it (check your web host FAQ page or ask support)
    I assume you're trying to set cron on your web host, not at home.
    Do you really need traffic? Where to? What for?
    If you really do need traffic then stop messing around!
    Advertise on my sites today: She Told Me & Best Reviewer :
    200,000+ UV / Month

  3. #3
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JamesColin View Post
    I assume you're trying to set cron on your web host, not at home.
    Yes, this is for a web server (VPS).

    Quote Originally Posted by JamesColin View Post
    Could it be because of your host config? Are you sure this is the way to do it
    I'm not sure what you mean by this, but I know virtually every CRON guide/tutorial on the internet says this is the way to do it. (crontab -e)

  4. #4
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lets assume it at home or dedicated server.
    The good way is to use cron under a user not root. But if your script need to run under root, then its fine you can do so.
    Now do it like this way:
    Enter to user/root account and type this:
    vim crontab -e
    OR
    vi crontab -e
    To check your cron entries:
    crontab -l
    Press the insert button from your keyboard to start typing.
    After finished typing, press Esc key to end typing mode and hold shift and press z twice to save the entries.
    And you are done!

    EDIT: i was a bit late to reply. you can try it on VPS too.

  5. #5
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mmarif4u View Post
    Lets assume it at home or dedicated server.
    The good way is to use cron under a user not root. But if your script need to run under root, then its fine you can do so.
    Now do it like this way:...
    Alright, I tried that, but all it did was create a file called crontab in the 'root' folder. 'crontab -l' does not list anything from this file.


    I have figured out that "crontab -l" lists the contents of "/var/spool/cron/root".

    I have also noticed that the command, "crontab /var/spool/cron/root", returns this:
    Code:
    "/var/spool/cron/root":2: bad minute
    errors in crontab file, can't install.
    Perhaps I am just doing it wrong...
    This is the command I am trying to schedule:
    Code:
    * * * * * /usr/local/bin/php /cron/encoding_processor.php > /dev/null 2>&1
    (this line is the only content inside '/var/spool/cron/root')

  6. #6
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1st thing you need to do is remove this from the line for cron to receive mails from cron to what see what exactly the error is(Just for a while), you can add it later.
    > /dev/null 2>&1
    Next is you need to use php-cgi instead of just php. Some thing like:
    * * * * * /usr/local/bin/php-cgi /cron/encoding_processor.php
    Next is you need to give the full path to your script.
    Next is to put this to debug your script every next minute for mails, to know the exact error.
    0-59/2 * * * * /usr/local/bin/php-cgi /cron/encoding_processor.php
    Make sure that php-cgi module is installed and where it is, you can find out this from phpinfo();
    You can run this command to find the exact path to php
    whereis php
    You have not to worry about the cron saved files. just make sure you inserted the correct lines and valid data. One more thing, better to run it as a user not root as i said above.

    EDIT: forgot to mention about mails:
    You can check cron mails by command:
    cat /var/spool/mails/username
    Note: sometime it looks like this way:
    cat /var/mails/username
    Change the username with exact username.

  7. #7
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Morthian View Post
    Code:
    "/var/spool/cron/root":2: bad minute
    errors in crontab file, can't install.
    I have fixed this error by disabling word wrap in the editor. The script now runs as scheduled.
    Now I just need to figure out why I am getting a 'cannot connect to database' error when trying to connect as 'root'.


    Quote Originally Posted by mmarif4u View Post
    Next is you need to use php-cgi instead of just php. ... Next is you need to give the full path to your script. ... One more thing, better to run it as a user not root as i said above.
    '/cron/encoding_processor.php' is the full path to the script.

    I am running the script as 'root' because it interacts with files and databases from all user accounts.
    Is there a significant reason why I should not use 'root'?

    Why do you suggest using 'php-cgi' rather than 'php'?

  8. #8
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, I fixed the database connection issue.

    I am still curious about the two questions at the end of my last post.

  9. #9
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Morthian View Post
    I have fixed this error by disabling word wrap in the editor. The script now runs as scheduled.
    Now I just need to figure out why I am getting a 'cannot connect to database' error when trying to connect as 'root'.




    '/cron/encoding_processor.php' is the full path to the script.

    I am running the script as 'root' because it interacts with files and databases from all user accounts.
    Is there a significant reason why I should not use 'root'?

    Why do you suggest using 'php-cgi' rather than 'php'?
    Its because of security issue, but as you said the script need to be run under root, can be done. Its totally your choice at this point. I personally run scripts under users username not root.

    This would help about php, php-cgi.
    http://www.htaccesselite.com/differe...ule-vt106.html

  10. #10
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mmarif4u View Post
    Its because of security issue, but as you said the script need to be run under root, can be done. Its totally your choice at this point. I personally run scripts under users username not root.

    This would help about php, php-cgi.
    http://www.htaccesselite.com/differe...ule-vt106.html
    How exactly is running a cron job as root a security issue? Is there another option besides running it as specific account user?

    Regarding 'php-cgi', should I use this instead of 'php' in exec functions as well? (when scripts execute other scripts)

  11. #11
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How exactly is running a cron job as root a security issue?
    It's not in and of itself. It can become an issue if the cron job works with any kind of untrusted input though. If you trust everything that the cron job will be working with, don't worry about it.

  12. #12
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by joebert View Post
    It's not in and of itself. It can become an issue if the cron job works with any kind of untrusted input though. If you trust everything that the cron job will be working with, don't worry about it.
    Exactly.
    In simple words: Running untrusted things under root mean inviting people to control your machine.

  13. #13
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Palm Harbor, FL
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fortunately, this script contains no user input whatsoever.

    I just cant seem to figure out how to execute scripts from user directories as the user who owns the directory. ...or at least access user directories with privileges equal to those of the owner.

    I tried 'sudo -u [username]' before the command, but I get this error:
    audit_log_user_command(): Connection refused


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •