+ Post New Thread
Results 1 to 15 of 15
*nix Thread, Script not runnning in crontab? in Technical; I seem to missing athe point somehwre in using crontab? I have a script which runs fine when I type ...
  1. #1
    reggiep's Avatar
    Join Date
    Apr 2008
    Location
    In the vast area of space and time
    Posts
    1,548
    Thank Post
    517
    Thanked 56 Times in 50 Posts
    Rep Power
    29

    Script not runnning in crontab?

    I seem to missing athe point somehwre in using crontab?

    I have a script which runs fine when I type ./script.sh as I have chmodded the file like this...

    chmod ugo+rx moodlebackup.sh

    but I can't get it to run in cron.

    I have gone in as root, typed crontab -e, and added this line...

    52 11 * * Mon-Fri /var/moodlebackup/moodlebackup.sh

    But nothing happens at 11:52!

    Can anyone make any suggestions. Thanks.

  2. #2


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339
    try
    Code:
    52 11 * * Mon-Fri /bin/sh /var/moodlebackup/moodlebackup.sh

  3. Thanks to CyberNerd from:

    reggiep (19th June 2009)

  4. #3

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    As an aside, you shouldn't place executables in /var (at least not in the FHS) - if it is mounted tmpfs, your script will be nuked when /var gets remounted. All binaries should be in

    /bin if they're base system
    /sbin if they're base system for the superuser's use
    /usr/bin if they're additional
    /usr/sbin if they're additional for the superuser's use

    Yours should probably go in /usr/bin. If you're going to do that, you should also remove the file extension and ensure you have a shebang at the top of the script (like #!/bin/sh).

  5. Thanks to powdarrmonkey from:

    reggiep (19th June 2009)

  6. #4
    reggiep's Avatar
    Join Date
    Apr 2008
    Location
    In the vast area of space and time
    Posts
    1,548
    Thank Post
    517
    Thanked 56 Times in 50 Posts
    Rep Power
    29
    @powdarmonkey.

    Am I OK to put the script in a subfolder so it would be /usr/bin/moodlebackups?

    Just so i can find it easier later!

    I also had #!/bin/bash at the top of my script rather than #!/bin/sh. Not sure if that would make a difference?

    Also do i have to take the sh off the end as at the moment that helps my small brain recognise what it is!

    Thanks

  7. #5
    reggiep's Avatar
    Join Date
    Apr 2008
    Location
    In the vast area of space and time
    Posts
    1,548
    Thank Post
    517
    Thanked 56 Times in 50 Posts
    Rep Power
    29
    Strange I don't see my commands in this crontab?
    I see this

    Code:
    # /etc/crontab: system-wide crontab
    # Unlike any other crontab you don't have to run the `crontab'
    # command to install the new version when you edit this file
    # and files in /etc/cron.d. These files also have username fields,
    # that none of the other crontabs do.
    
    SHELL=/bin/sh
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    
    # m h dom mon dow user  command
    17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
    25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
    47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
    52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
    #
    I typed crontab -e while logged on as root so I am guessing that the crontab I edited was for the root user. How could I edit this system crontab.

    Also I have my script running now thanks to powdarmonkey's suggestions but now I am adding firther problems by trying to get my moodlecron.php to run.
    It is suggested by moodle that I type place
    Code:
    */5 * * * * /usr/local/bin/php -q /var/www/moodle/admin/cron.php
    in the crontab which I have done and now this isn't running but my backup one is?

    Any suggestions?

    thanks

  8. #6
    reggiep's Avatar
    Join Date
    Apr 2008
    Location
    In the vast area of space and time
    Posts
    1,548
    Thank Post
    517
    Thanked 56 Times in 50 Posts
    Rep Power
    29
    I used to edit the crontab like you suggested until I read a shortcut, doh! Not that useful a shortcut.

  9. #7

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    Ok, there's some confusion here. /etc/crontab is the system crontab, and can run jobs on a user's behalf but can only be changed by root. If user-based schedules are also allowed, crontab -e allows them to be edited by the user (no need to involve root).

    If you're going to schedule system-wide jobs, it's good practice to put them in /etc/crontab because you don't need to be root to see them. Saying cat /etc/crontab as a user reveals the jobs, avoiding needing to authenticate and so on. If they are private, putting them in root's user crontab maintains this.

    (The user crontabs are actually stored in /var/spool/cron/crontabs, but you must never edit them by hand - always use crontab -e, which does it safely. The system crontab is fair game for the editor of your choice, and cron picks the changes straight up.)

    You can store your script in a subdirectory of /usr/bin if you like, but the subdirectory is not searched as part of $PATH. To keep everything sane, put your script and associated resources in a subdirectory of /usr/local if they are architecture-dependent binaries, or /usr/local/share for architecture-independent stuff (/usr and /usr/share on Debian systems) and create symlinks for the executable bits in /usr/bin.

  10. #8


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339
    Am I OK to put the script in a subfolder so it would be /usr/bin/moodlebackups?
    All locally installed software, that isn't part of the operating system should be in /usr/local or /opt
    this stops it from being overwritten with os updates, etc
    as posted above, put it in /usr/local then symlink it to /usr/local/bin IMO

  11. #9
    reggiep's Avatar
    Join Date
    Apr 2008
    Location
    In the vast area of space and time
    Posts
    1,548
    Thank Post
    517
    Thanked 56 Times in 50 Posts
    Rep Power
    29
    Quote Originally Posted by CyberNerd View Post
    All locally installed software, that isn't part of the operating system should be in /usr/local or /opt
    this stops it from being overwritten with os updates, etc
    as posted above, put it in /usr/local then symlink it to /usr/local/bin IMO
    Ok I have moved the script to /usr/local as suggested.

    Can you explain what you mean by symlink it?

    Thanks

  12. #10


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339
    Code:
    sudo ln -s /usr/local/subdirectory/script.sh /usr/local/bin/script.sh
    this puts a 'shortcut' (symbolic link) into /usr/local/bin
    as /usr/local/bin is in your path, you now don't need type the full path to run it (same as putting script in /usr/bin)

  13. #11
    reggiep's Avatar
    Join Date
    Apr 2008
    Location
    In the vast area of space and time
    Posts
    1,548
    Thank Post
    517
    Thanked 56 Times in 50 Posts
    Rep Power
    29
    Wow the more I do the less I know!

    Would I just run that at the command line, Cybernerd, or would I put that into my crontab?

    Just out of interest this is jnow my system crontab that at this moment is not working!

    Code:
    # /etc/crontab: system-wide crontab
    # Unlike any other crontab you don't have to run the `crontab'
    # command to install the new version when you edit this file
    # and files in /etc/cron.d. These files also have username fields,
    # that none of the other crontabs do.
    
    SHELL=/bin/sh
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/bin/moodlebackup
    
    # m h dom mon dow user  command
    17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
    25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
    47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
    52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
    33 14 * * Mon-Fri  /bin/sh /usr/local/moodlebackup/moodlebackup.sh > /var/log/moodlebackup.log 2>&1
    */5 * * * * curl --silent --compressed http://www.prentonmoodle.co.uk/admin/cron.php > /var/log/moodlecron.log 2>&1

  14. #12


    Join Date
    Jan 2006
    Posts
    8,202
    Thank Post
    442
    Thanked 1,032 Times in 812 Posts
    Rep Power
    339
    33 14 * * Mon-Fri /bin/sh /usr/local/moodlebackup/moodlebackup.sh > /var/log/moodlebackup.log 2>&1
    */5 * * * * curl --silent --compressed http://www.prentonmoodle.co.uk/admin/cron.php > /var/log/moodlecron.log 2>&1
    I think you need to specify the user

  15. #13

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    You've missed the user specification out of your two additional lines. This is one of the major differences between the system table and the user tables. (Read the file header comment carefully.)

  16. #14
    reggiep's Avatar
    Join Date
    Apr 2008
    Location
    In the vast area of space and time
    Posts
    1,548
    Thank Post
    517
    Thanked 56 Times in 50 Posts
    Rep Power
    29
    I'm not just saying this but I spotted that as soon as I posted it, honest!
    I have added root to the user, so I'll give it another whirl.

    thanks

  17. #15
    reggiep's Avatar
    Join Date
    Apr 2008
    Location
    In the vast area of space and time
    Posts
    1,548
    Thank Post
    517
    Thanked 56 Times in 50 Posts
    Rep Power
    29
    All seems to be going ok now.

    Thanks to everybody who helped me out.

SHARE:
+ Post New Thread

Similar Threads

  1. [Ubuntu] crontab not working
    By Hightower in forum *nix
    Replies: 4
    Last Post: 17th March 2009, 10:24 AM
  2. Need a Script if Possible
    By Paul_L in forum Scripts
    Replies: 6
    Last Post: 2nd October 2008, 02:33 PM
  3. Replies: 0
    Last Post: 30th September 2008, 03:24 PM
  4. Logon script & Printer script question.
    By Galway in forum Windows
    Replies: 3
    Last Post: 29th August 2007, 11:00 AM
  5. Does anyone have a script...
    By wesleyw in forum Scripts
    Replies: 4
    Last Post: 5th July 2007, 01:58 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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