LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-06-2009, 12:35 PM   #1
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Rep: Reputation: 31
How crontab works?


Hello.
I tried just for test and to learn how crontab works, this example.
I created new line in crontab like this:

Code:
*/1 * * * *	miros	/home/miros/test1
So, I suppose every minute, will be execute the file test1 in my folder miros

And this file test1 has this command

Code:
mkdir "/media/Multimedia/NVIDIA/$(DATE)"
So, what I want is create one folder each minute with name depend on the date.
But nothing happned.
What is wrong what I am doing?
 
Old 09-06-2009, 12:49 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Which crontab is it? If it is miros' crontab, you don't need miros as the 6th field; cron will try to run miros as a command.
 
Old 09-06-2009, 01:01 PM   #3
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
Try to put the following in test1

Code:
#! /bin/bash
/bin/mkdir /media/Multimedia/NVIDIA/"`date`"
Make sure the file is executable
Code:
chmod +x /home/miros/test1
You can look in the logfiles for errors, or in the mails sent to root

Last edited by repo; 09-06-2009 at 01:02 PM.
 
Old 09-06-2009, 01:14 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by repo View Post
Try to put the following in test1

Code:
#! /bin/bash
/bin/mkdir /media/Multimedia/NVIDIA/"`date`"
Make sure the file is executable
Code:
chmod +x /home/miros/test1
You can look in the logfiles for errors, or in the mails sent to root
The original
Code:
mkdir "/media/Multimedia/NVIDIA/$(DATE)"
is equivalent to
Code:
/bin/mkdir /media/Multimedia/NVIDIA/"`date`"
If the cron job is being run for miros then wouldn't the mail detailing any errors be sent to miros rather than root?

mimiros84, have you tested the test1 script by running it from the command line?
 
Old 09-06-2009, 01:23 PM   #5
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
Cron has a limited PATH, so best to give the whole path to mkdir
Code:
mkdir "/media/Multimedia/NVIDIA/$(DATE)"
will only work if the variable $DATE is set in the script

Code:
/bin/mkdir /media/Multimedia/NVIDIA/"`date`"
will work without any variables
 
Old 09-06-2009, 03:52 PM   #6
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by repo View Post
Try to put the following in test1

Code:
#! /bin/bash
/bin/mkdir /media/Multimedia/NVIDIA/"`date`"
Make sure the file is executable
Code:
chmod +x /home/miros/test1
You can look in the logfiles for errors, or in the mails sent to root
You are great. I made these changes and now it works!!!

If I have some mails, where can I look for them?

catkin, Yes it is Miros'crontab but it works. I changed only test1. Now I have it:

in test1

Code:
#! /bin/bash
/bin/mkdir /media/Multimedia/NVIDIA/"`date`"
and in crontab for miros

Code:
*/1 * * * *	miros	/home/miros/test1
Can you tell me why I need to put /bin before the command?
And this #! /bin/bash is like coment right? There is # before. So, it is not need?

Last edited by miros84; 09-06-2009 at 03:57 PM.
 
Old 09-06-2009, 03:57 PM   #7
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
Quote:
Can you tell me why I need to put /bin before the command?
Because Cron has a limited PATH
Quote:
And this #! /bin/bash is like coment right? There is # before. So, it is not need?
It is called a shebang. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash.
All scripts under UNIX and Linux execute using the interpreter specified on a first line.
It is needed in every script.

For more info on bash
http://tldp.org/LDP/Bash-Beginners-Guide/html/
Quote:
If I have some mails, where can I look for them?
as user type
Code:
mail

Last edited by repo; 09-06-2009 at 04:02 PM.
 
1 members found this post helpful.
Old 09-06-2009, 04:01 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982Reputation: 1982
Quote:
Originally Posted by miros84 View Post
Can you tell me why I need to put /bin before the command?
Well, the advice is to put the full path of every command in crontab jobs. This is because the environment of crontab is different from that one you get at login. Try to put a simple
Code:
/bin/echo $PATH
in your cron job and you will discover that PATH is very limited, usually to /bin:/usr/bin. Hence a full /bin path is not really needed, but it's a good habit to avoid headache in the future!
Quote:
And this #! /bin/bash is like coment right? There is # before. So, it is not need?
Nope. Even if it starts with a hash, it's not a comment. You have to put it in the very first line of your scripts and it tell which interpreter must be used to run the subsequent statements. It is called sha-bang and has the advantage that make your script more portable, forcing to interpret commands in a particular shell environment. Also you can add options as if you run the script as (just an example):
Code:
bash -x script.sh
Edit: too slow... sorry for redundancy, repo!

Last edited by colucix; 09-06-2009 at 04:02 PM.
 
1 members found this post helpful.
Old 09-06-2009, 04:06 PM   #9
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
Quote:
Edit: too slow... sorry for redundancy, repo!
No problem
LQ Addict wannabe! -)
 
Old 09-06-2009, 04:15 PM   #10
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
Thank you for helping and for explain.
Now I can make some real jobs as copy some important folder from my computer to another in my home network.
 
Old 09-06-2009, 05:06 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by repo View Post
Cron has a limited PATH, so best to give the whole path to mkdir
Code:
mkdir "/media/Multimedia/NVIDIA/$(DATE)"
will only work if the variable $DATE is set in the script

Code:
/bin/mkdir /media/Multimedia/NVIDIA/"`date`"
will work without any variables
My mistake -- $(DATE) will try to run an executable called DATE. See Command Substitution in the GNU Bash Reference Manual. I should have said that /media/Multimedia/NVIDIA/"`date`" is equivalent to "/media/Multimedia/NVIDIA/$(date)". It doesn't matter if /media/Multimedia/NVIDIA is inside or outside the quotes because it has no whitespace characters within it.

One thing I don't understand -- if it was necessary to use /bin/mkdir, why was it not also necessary to use /bin/date?
 
Old 09-07-2009, 02:06 AM   #12
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
Quote:
One thing I don't understand -- if it was necessary to use /bin/mkdir, why was it not also necessary to use /bin/date?
IMHO the problem was
1 test1 wasn't a script, just a txt file containing mkdir "/media/Multimedia/NVIDIA/$(DATE)"
2 Since it wasn't a script, it wasn't executed
3 mkdir "/media/Multimedia/NVIDIA/$(DATE)" doesn't work, since the variable isn't set, hence it wasn't even a script, and the syntax isn't correct
4 mkdir /media/Multimedia/NVIDIA/"`date`" works, without variables, even from the command line
5 /bin is in the path from cron, but I always use full path in cron.
 
Old 09-07-2009, 03:09 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by repo View Post
3 mkdir "/media/Multimedia/NVIDIA/$(DATE)" doesn't work, since the variable isn't set, hence it wasn't even a script, and the syntax isn't correct
$(<command(s)>) is the preferred successor to `<command(s)>` except for quick-and-dirty use at the command line. The reference information is on the link above. The reasons why it is preferred are in the Advanced Bash Scripting Guide, search for "The $(...) form has superseded backticks for command substitution"
Quote:
Originally Posted by repo View Post
5 /bin is in the path from cron, but I always use full path in cron.
Ah, that's good to know. So there was no need to use /bin/mkdir but I agree with you that it is good practice, both in scripts to be run from cron and to ensure the intended executable is run, regardless of $PATH. One downside of this approach is that it may cause breakage when running on a variety of distros when executables may not be in the same directory; for this reason I'm wondering if it may be better to set $PATH in the script than to use executable full paths.
 
Old 09-07-2009, 07:45 AM   #14
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
Well, so If I want to create a script. How can I do it? What I want to do is autocopy a folder from one computer to other. So is that ok?

Code:
#! /bin/bash
namefolder=$(date); mkdir "/media/Multimedia/newexpbackup/$namefolder"; tar -zcvf "/media/Multimedia/newexpbackup/$namefolder/backup.tar.gz" /home/miros/Myboxfiles
When I put this command in the bash, it works. How can I do it as script? What is the diferent between scripts and bash commands?
I tested the script above and it works.

Last edited by miros84; 09-07-2009 at 07:51 AM.
 
Old 09-07-2009, 10:05 AM   #15
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
You can compare a script with the batch file in windows.
It can contain different commands, which will be executed one by one.
It is easier to write a script, then to type the command each time at the command prompt.
You can add comments in the script, so after one year you still know where the script is for, and what it does.

If you use a script, no use to put all the commands in one line

Code:
#! /bin/bash
# script to backup my files

# Set some variables
namefolder=$(date)

#Create the directory
mkdir "/media/Multimedia/newexpbackup/$namefolder"

# backup the files
tar -zcvf "/media/Multimedia/newexpbackup/$namefolder/backup.tar.gz" /home/miros/Myboxfiles

# another job well done
A scripts are a very powerfull tool to automate your daily tasks

Last edited by repo; 09-07-2009 at 10:06 AM.
 
  


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
linux crontab vs unix crontab ytd Linux - General 2 08-09-2009 05:07 AM
crontab doesn't works heathcliffz Linux - Server 10 07-05-2007 06:19 AM
Shell pgm works fine in command line but not in Crontab oracledba_raja Linux - General 2 11-24-2006 12:14 AM
shell script works form command line but not form crontab saifee General 1 10-14-2004 10:27 AM
crontab doesnt works with all jobs. pxes351 Linux - Newbie 2 05-10-2004 01:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:13 AM.

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
Open Source Consulting | Domain Registration