LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-07-2008, 06:53 AM   #1
caksin
Member
 
Registered: Jul 2008
Location: Ankara, Türkiye
Distribution: Ubuntu, Pardus
Posts: 47

Rep: Reputation: 16
Can't make cronjobs work


in this directory,/var/spool/cron/crontabs, there are 2 files; 1 is named as the username (penguen1) and the other is "root". i have tried it on both, what is wrong with what i am trying to do it everyday?

# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXcRiecM installed on Wed Aug 6 16:05:06 2008)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)

21 13 * * * root cd /home/penguen1/Desktop/oto tar -cvf f.tar /home/penguen1/yedekleme/rdiff

must i use any punctiations like ";" between different commands? i tried ":" after oto (the directory to backup using tar), it didn't work as well
please help

What would the format be if i tried to do it by placing under /etc/cron.daily ?
 
Old 08-07-2008, 07:25 AM   #2
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
hi. TRY
21 13 * * * `cd /home/penguen1/Desktop/oto ; tar -cvf f.tar /home/penguen1/yedekleme/rdiff`
 
Old 08-07-2008, 07:25 AM   #3
bgey
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Rep: Reputation: 0
Hm...

1) this is on a Linux system?

2) You can use the command crontab -e to edit your crontab file.

3) ';' is the character to use to have multiple commands on a single line in a script. Don't know if this works in a cron file though...

4) why not put those commands in a small script and then run that script from the crontab?

5) if doing 2) -> don't put the user in (root in your example)

Cheers!

Benny
 
Old 08-07-2008, 07:26 AM   #4
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
You should edit cronjobs as the user with "crontab -e" rather than trying to edit these files

The best strategy is to create a script and to call that from your crontab, so for example, in /home/penguen1/cron1.sh

Code:
#!/bin/bash
cd /home/penguen1/Desktop/oto
tar -cvf f.tar /home/penguen1/yedekleme/rdiff
Make it executable with
Code:
chmod u+x /home/penguen1/cron1.sh
then as user penguen1, run the command crontab -e

add the line

Code:
21       13       *       *       *       /home/penguen1/cron1.sh

Last edited by billymayday; 08-07-2008 at 07:35 AM.
 
Old 08-07-2008, 09:01 AM   #5
caksin
Member
 
Registered: Jul 2008
Location: Ankara, Türkiye
Distribution: Ubuntu, Pardus
Posts: 47

Original Poster
Rep: Reputation: 16
hi thank u all. I have tried what is in the first reply but didn't work. This is a linux distro: pardus. I also tried it without the "root" user, result: same. So i will try to make it with a script as you told me to billymayday. But i don't know if it's ok if i just make a text file and change the file's name to make it smthing.sh . I will to try to make it this way until you reply. I will also try copying a .sh file and changing its content.
BTW i forgot to state in my first post that i tried it by using crontab -e before what i told i tried here.
 
Old 08-07-2008, 09:09 AM   #6
bgey
LQ Newbie
 
Registered: Aug 2008
Posts: 4

Rep: Reputation: 0
To make a script work you should give that file the correct permissions, i.e.

> chmod o+rwx scriptname.sh

Cheers!

Benny
 
Old 08-07-2008, 09:47 AM   #7
caksin
Member
 
Registered: Jul 2008
Location: Ankara, Türkiye
Distribution: Ubuntu, Pardus
Posts: 47

Original Poster
Rep: Reputation: 16
Here is what i did;
at /home/penguen1 right click on white space > new > text file
and here is what i wrote in it;

#!/bin/bash
cd /home/penguen1/Desktop/oto
tar -cvf fdfdfd.tar /home/penguen1/yedekleme/rdiff

i changed the name of the file to sdf.sh

Then on the command line (yakuake for pardus) (btw do i need to become root to set the cron job?)
>crobtab -e
39 16 * * * /home/penguen1/sdf.sh
then ctrl+x and "y" and "enter"
it says installing new crontab

but the cronjob still doesn't work although when i double click on the sdf.sh it does do the job.
what can i do?

One last thing; i must stop the mysql server and restart it after the tar job. is it okay if i add the "service mysql_server stop" to the top afer the bin/bash and "service mysql_server start" to the end of the script (sdf.sh) Will there be enough time for mysql_server to stop before tar backups? because it takes like 3 seconds for mysql_server to stop
One more last thing: i should add someting (preferably date) to the end of the backup file name (sdf+smth.sh) so that the previous backup is not deleted each time. How can i do it?

Last edited by caksin; 08-07-2008 at 10:51 AM.
 
Old 08-07-2008, 12:44 PM   #8
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Stopping/starting services can only be done by root. If you add the mysql lines, you should put the script in root's crontab, not penguen1's (ie first change to root, then crontab -e and don't forget to remove it from penguen1's crontab). If you leave out the mysql lines, you can probably run it as penguen1 and therefore leave it in penguen1's crontab.

Note that it can take a minute for cron to see the changes you make. How did you tell if the job worked or not (either manually or from cron)? I also wonder what you're trying to tar that requires stopping mysql first? Your database itself? Perhaps a mysqldump is suitable?

Scripts are text files with commands in them. They require a specific syntax, depending on the scripting language (Bash, sh, ksh, Tcl, Perl, Python,...). They also need an interpreter program, like /bin/bash for Bash-style scripts, which is put at the top in the shebang (#!) line. If no shebang line is put in, the current shell of the user that runs the script is used (ie the shell you're working in). Your "default" or login shell is stored in /etc/passwd and can be changed with the "chsh" (CHange login SHell) command.

As for the time required to stop mysql: this depends if you run the "service mysql stop" command in background or not.
If in the background (ie "service mysql stop &"), the cd and tar commands are started without waiting for the stop of mysql to complete. If not in the background ("service mysql stop" without ampersand), then the script will wait with the "cd" command until the mysql has stopped.

To add the data, use something like:
Code:
today=`date +%Y%m%d`; tar -cvf fdfdfd_${today}.tar /home/penguen1/yedekleme/rdiff
I suggest you read up on Bash, in particular the use of quoting and on job management.
 
Old 08-07-2008, 04:41 PM   #9
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
You will may need the full path for command like tar, so something like

/bin/tar

instead of just tar
 
Old 08-08-2008, 05:36 AM   #10
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
You're right, billymayday.
Same goes for the "date" command too: /bin/date
 
Old 08-08-2008, 06:38 AM   #11
caksin
Member
 
Registered: Jul 2008
Location: Ankara, Türkiye
Distribution: Ubuntu, Pardus
Posts: 47

Original Poster
Rep: Reputation: 16
"yes i am trying to backup a mysql db. I tried to use mysqldump but it says command not found when i try to use it.
i also tried mysqlhotcopy and do not remember what error came up. I can not try anything during the work hours here, i can try only after 18:00 here. (At the time of post it is 13:10 here)So i can not remember now the error.
I did learn mysqldump is better but the pc i must do the auto backup has fedora core 4 on it and it is harder for me to understand anything about it. So i don't know what to do when it says command not found after typing mysqldump and enter.
if stopping and starting the mysql_server job must be done by the root user, and if i just want to use the .sh file until i am able to do it with cron, is there anything i can add to the script to switch to root? How can i do it with the root with the cron?
as sa * * * root commands. is enough?
must i put the password as well? how?
I would really appriciate it if u helped me do this, i am new to linux and still the boss expects me to achieve auto backup right off. This is my 3rd thread here, but evertime i learn smth to do, a new problem comes up, so i still haven't been able to make it happen.
I think i will try to do it with the tar + cron for now since i have no idea how to fix mysqldump- mysqlhotcopy errors. My final position is;
when i double click on the sdf.sh file which has
"
#!/bin/bash
today=`date +%Y%m%d`
cd /home/penguen1/Desktop/oto
tar -cvf sdf_${today}.tar /home/penguen1/yedekleme/rdiff
"
in it, it does make a tarball(if this is what it is called) in the dest. folder with the date of the day attached to "sdf_".
but when i add a cronjob with "crontab -e" using the line;
09 11 * * * /home/penguen1/sdf.sh
nothing happens. Is this time stated here same with what is written on the taskbar clock?

Last edited by caksin; 08-08-2008 at 07:04 AM.
 
Old 08-08-2008, 01:05 PM   #12
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
If you ask a lot of questions, you should number them to make answering easier.

1. mysqldump gives you "command not found" issue
Have you specified the correct path? If you don't give a path (ie just type "mysqldump"), then Linux will search all directories in $PATH (do a "echo $PATH" to see them). If it doesn't find the command in any of those directories, you get the message "command not found". If you installed mysql as rpm package, you can use a command like "rpm -q --filesbypkg mysql" to find where it's installed.

2. Don't bother about the time. People contribute voluntarily to this forum, whenever *they* have time. So don't expect to see a response when it's convenient for you.

3. To run the job as root, there are a few approaches:
-switch to root, then do "crontab -e" to put the job in root's crontab settings
-configure "sudo" to make penguen1 able to run the job as if he was root
-change the ownership of the .sh file to "root" and then set the special setUID permission
I recommend sticking to the first option since you're new to Linux.
Adding to root's crontab is similar as for the other users:
Code:
#switch to root - it'll ask for the password
su -
#edit root's crontab
crontab -e
#now add the cron job, same format as usual, ie
39 16 * * * /home/penguen1/sdf.sh
#exit the editor and save the changes you made, then check if it worked
4. Please read billymayday's post: you should give full path's to the commands used in the .sh file. Not just "tar" and "date", but "/bin/tar" and "/bin/date". This is because cron jobs run in their own special environment, which is different from penguen1's standard environment.

5. As I told you in my earlier post, you should allow cron a little time to ingest the changed settings. So don't put the system time (from the taskbar clock) into the crontab settings. Rather, use a time that's still a few minutes away.
ie if it's now 16h, use something like 05 16 * * * /your/job
Alternatively, for testing only, you could have cron run the job each minute, ie:
* 16 * * * /your/job
 
Old 08-08-2008, 04:10 PM   #13
hasanatizaz
Member
 
Registered: Nov 2007
Location: Pakistan
Distribution: Redhat and Debian
Posts: 302
Blog Entries: 1

Rep: Reputation: 34
make sure crond service is running
service crond status
 
Old 08-14-2008, 04:56 AM   #14
caksin
Member
 
Registered: Jul 2008
Location: Ankara, Türkiye
Distribution: Ubuntu, Pardus
Posts: 47

Original Poster
Rep: Reputation: 16
I have been able to do it, and would like to post it so that others may benefit.
i created a .sh file by creating a text file and changing its name to something.sh with;

#!/bin/bash
today=`date +%Y%m%d`
cd /home/penguen1/Desktop/oto
tar -cvf sdf_${today}.tar /home/penguen1/yedekleme/rdiff

in it and placed it under cron.daily folder can not remember where the folder is now. I don't know now but i could not do it using crontab -e. I made the file something like executable by right clicking on it and putting a check(can't remember what it wrote there) so that it would work when you double click on it.
It does really take about two minutes cron to find out about the job. So try a few minutes after the clock on the taskbar when you testing.
I adjusted the time of the cron.daily jobs from the crond file in the same folder with the job folders (cron.daily,cron.weekly,...) by opening it with a text editor.
thank you all

Last edited by caksin; 08-14-2008 at 04:59 AM.
 
Old 08-14-2008, 09:06 AM   #15
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
If user's crontab settings (crontab -e) are not working, it may be useful to check out
/etc/cron.allow (list of users that are allowed to use cron) and/or /etc/cron.deny files. Same goes for the
status of the cron service, as in geniushasan's post.
The cron.daily,... folders are usually in /etc/ or in /etc/cron.d/.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't make cronjobs work caksin Linux - Newbie 2 08-07-2008 09:49 PM
some cronjobs don't work xpucto Linux - Newbie 5 05-05-2008 07:25 AM
1st time installing redhat - Make and Make Install does not work runlikeanantelope Linux - Newbie 4 02-19-2007 04:58 PM
How do I make a change to a current kernel? Would 'make oldconfig' work... jtp51 Slackware 11 11-02-2004 12:02 AM
'make' and 'make install' commands dont work on my system? ginda Linux - Newbie 9 04-18-2004 12:17 PM


All times are GMT -5. The time now is 04:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration