Jump to content

Recommended Posts

Posted

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.:D

Posted

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).

  • Thanks 1
Posted

@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

Posted

Strange I don't see my commands in this crontab?

I see this

 

# /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

*/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

Posted

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.

Posted
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

Posted
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

Posted

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)

Posted

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!

 

# /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

Posted
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.)
Posted

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...