Fedora Linux Support Community & Resources Center
  #1  
Old 1st March 2012, 10:40 AM
eaglelinux's Avatar
eaglelinux Offline
Registered User
 
Join Date: Jun 2011
Posts: 158
linuxchrome
Question Automatic script execution !

Hello !

How to make an " Automatic script execution " time based.
For example: Execute automaticly " backup.sh " everyday at 00:00 o'clock.

Thanks !
Reply With Quote
  #2  
Old 1st March 2012, 11:10 AM
glennzo's Avatar
glennzo Online
Un-Retired Administrator
 
Join Date: Mar 2004
Location: Salem, Mass USA
Posts: 13,922
linuxchrome
Re: Automatic script execution !

I created a cron job for a specific task that I want to run once a day. As I recall, after a bit of research, all I did was add to /etc/crontab, like this:

Code:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
* 1 * * * root /usr/bin/rkhunter --propupd
The red line is, probably most obvious, the line I added. Given my understanding I assume that this will run every day at 1AM. This is my server so it's up 24/7. I believe that I needed to restart the crond service.
PHP Code:
systemctl restart crond.service 
My system logs tell me that this works as expected.
__________________
Glenn
The Bassinator © ®


Laptop: Toshiba Satellite / Intel Core 2 Duo 1.73 GHz / 2GB / 160GB / Intel Mobile 945GM/GMS/GME/943/940GML Integrated Graphics
Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 4GB / 1TB SATA / 500GB SATA / EVGA GeForce 8400 GS 1GB
Reply With Quote
  #3  
Old 1st March 2012, 11:24 AM
stevea's Avatar
stevea Online
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,298
linuxfirefox
Re: Automatic script execution !

Also check out 'anacron' which fits some applications better.
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe
Reply With Quote
  #4  
Old 1st March 2012, 11:30 AM
Adunaic's Avatar
Adunaic Offline
Registered User
 
Join Date: Mar 2009
Location: Lancaster, UK
Posts: 883
linuxfirefox
Re: Automatic script execution !

You could also use crontab. This is user based.

to add/ edit a crontab entry do

Code:
crontab -e
to list current ones do

Code:
crontab -l
syntax for the jobs is the same as for cron in @bobs post.

Also - bewarned - crontab is very scary at first due to the fact that it uses 'vi'. To change this, type:

Code:
EDITOR=nano  #if you want nano
EDITOR='emacs -nw' #if you want emacs in terminal mode
EDITOR=emacs #if you want full emacs mode
EDITOR=vi # to change it back to vi
or any other editor you like.
Reply With Quote
  #5  
Old 2nd March 2012, 12:08 AM
sea's Avatar
sea Offline
"Shells" (of a sub world)
 
Join Date: May 2011
Location: Helvetic Federation (Swissh)
Age: 33
Posts: 2,597
linuxfedorafirefox
Re: Automatic script execution !

Oh crontab uses vi?
Never figured that one, as i simply did:

Code:
# Get current jobs:
  tmpfile=/tmp/cronjobs
  crontab -l > $tmpfile
# Edit the file
  gedit $tmpfile
# Save it back
  crontab $tmpfile
Replace gedit with any editor of your choice.
__________________
Fedora Manual: http://docs.fedoraproject.org
Script-Tools: https://sourceforge.net/projects/script-tools/
sudo st tweak repo toggle livna ; st iso dl-fed && st iso usb
Reply With Quote
  #6  
Old 2nd March 2012, 12:20 AM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,089
linuxfirefox
Re: Automatic script execution !

I prefer having a backup of the crontab file anyway.

It works either way, but with a backup, I can restore the system and still not worry about loosing a crontab edit pass between backup and failure...
Reply With Quote
  #7  
Old 2nd March 2012, 09:46 AM
Adunaic's Avatar
Adunaic Offline
Registered User
 
Join Date: Mar 2009
Location: Lancaster, UK
Posts: 883
linuxfirefox
Re: Automatic script execution !

Quote:
Originally Posted by sea View Post
Oh crontab uses vi?
Never figured that one, as i simply did:

Code:
# Get current jobs:
  tmpfile=/tmp/cronjobs
  crontab -l > $tmpfile
# Edit the file
  gedit $tmpfile
# Save it back
  crontab $tmpfile
Replace gedit with any editor of your choice.
Oh sea. My oppinion of you just dropped a little - you wriet all those scripts and use gedit? Im not saying it has to be emacs, nano or vi. (cat and sed are acceptable editors!!!)
Reply With Quote
  #8  
Old 2nd March 2012, 11:44 AM
sea's Avatar
sea Offline
"Shells" (of a sub world)
 
Join Date: May 2011
Location: Helvetic Federation (Swissh)
Age: 33
Posts: 2,597
linuxfirefox
Re: Automatic script execution !

Well to write scripts, i prefer easy marking and copy-paste.
And honestly, i cant friendly with emacs, tried a few times, but gedit is just much less than 1 minute setup and fits all my needs.
But hey, everyone has his own choice preferences.
__________________
Fedora Manual: http://docs.fedoraproject.org
Script-Tools: https://sourceforge.net/projects/script-tools/
sudo st tweak repo toggle livna ; st iso dl-fed && st iso usb
Reply With Quote
  #9  
Old 3rd March 2012, 06:27 PM
RupertPupkin's Avatar
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 4,612
linuxfedorafirefox
Re: Automatic script execution !

Quote:
Originally Posted by Adunaic View Post
cat and sed are acceptable editors!!!
Bah, real programmers use ed; it's the standard text editor!

Like you I'm surprised that sea writes his scripts in gedit. I just assumed he used Microsoft Word with the Comic Sans font.

Actually that might be an interesting thread: what editor and font people use for programming. Emacs and DejaVu Sans Mono are of course the only correct answers, but it would be fun to see all the incorrect ones!
__________________
OS: Fedora 18 x86_64 | CPU: AMD64 3700+ 2.2GHz | RAM: 2GB PC3200 DDR | Disk: 160GB PATA | Video: ATI Radeon 7500 AGP 64MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Realtek 8110SC
Reply With Quote
  #10  
Old 3rd March 2012, 07:21 PM
Adunaic's Avatar
Adunaic Offline
Registered User
 
Join Date: Mar 2009
Location: Lancaster, UK
Posts: 883
linuxsafari
Re: Automatic script execution !

Quote:
Originally Posted by RupertPupkin View Post
Bah, real programmers use ed; it's the standard text editor!

Like you I'm surprised that sea writes his scripts in gedit. I just assumed he used Microsoft Word with the Comic Sans font.

Actually that might be an interesting thread: what editor and font people use for programming. Emacs and DejaVu Sans Mono are of course the only correct answers, but it would be fun to see all the incorrect ones!
No. Proper programmers use echo and get it right first time!!
Reply With Quote
  #11  
Old 3rd March 2012, 08:53 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,089
linuxfirefox
Re: Automatic script execution !

Of course, those using echo have to deal with various keyboard issues as well...

But then, they also know how to use head/tail commands to delete lines too
Reply With Quote
  #12  
Old 4th March 2012, 12:33 AM
Adunaic's Avatar
Adunaic Offline
Registered User
 
Join Date: Mar 2009
Location: Lancaster, UK
Posts: 883
linuxsafari
Re: Automatic script execution !

I had not thought about using head and tail to delete lines.
Reply With Quote
  #13  
Old 4th March 2012, 02:37 AM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,089
linuxfirefox
Re: Automatic script execution !

One random puzzle challenge that came up at work (not a real problem) was how to do random record reads using the bourn shell.

The easy solution was "head -<number> file | tail -1" and that would be the record. The tail utility on Linux has the ability to do both at once, and simplifies the command considerably.

But editing a file still requires separate head/tail unless you use a rather complex awk script to insert the new data.

At one time, I had a cross reference tool for fortran that generated a "call by" table, and a "called by" table. The simplified parser only had to recognize subroutine calls (first pass identified function/subroutine names, then a second passes generated the lists. A third pass would collate the tables and generate the output, all using bourn shell scripts, awk, comm, and sort. Much faster than doing it by hand for required documents for a porting operation on an old HP3000 system.
Reply With Quote
  #14  
Old 4th March 2012, 07:24 AM
stevea's Avatar
stevea Online
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,298
linuxfirefox
Re: Automatic script execution !

Quote:
Originally Posted by jpollard View Post
One random puzzle challenge that came up at work (not a real problem) was how to do random record reads using the bourn shell.

The easy solution was "head -<number> file | tail -1" and that would be the record. The tail utility on Linux has the ability to do both at once, and simplifies the command considerably.
NO doesn't. 'tail' always prints the last N lines. N can be distance from the bottom or top, but it can't select a middle line.

Quote:
But editing a file still requires separate head/tail unless you use a rather complex awk script to insert the new data.
sed -n 5,+0p /etc/fstab # print line 5
sed -n 5,7p /etc/fstab # print lines 5-7 inclusive
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe
Reply With Quote
  #15  
Old 4th March 2012, 12:12 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,089
linuxfirefox
Re: Automatic script execution !

Steve, you missed the word "insert". Also that sed is an editor.

And from the manpage on tail:
Quote:
-n, --lines=K
output the last K lines, instead of the last 10; or use -n +K
to output lines starting with the Kth
Granted, I haven't tried it as that would seem a bit obscure.

BTW, "head -n" prints the first n lines of a file, "tail -1" prints the last line of the input. So "head -n | tail -1" prints only one line.

... seems the manpage for tail is flat out wrong. All it gives is a "invalid context" for the "or use" entry.

Last edited by jpollard; 4th March 2012 at 01:03 PM.
Reply With Quote
Reply

Tags
automatic, execution, script

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Example, using `pgrep` to limit script execution to one instance dstewart Programming & Packaging 1 7th January 2011 03:23 PM
script execution before reboot hal9000a Using Fedora 4 3rd September 2010 04:18 PM
Enabling script execution on a flash disk on fc6. enhering Using Fedora 0 26th November 2006 12:03 AM
Schedule Execution of a perl script jonmor Using Fedora 3 3rd August 2005 02:24 PM


Current GMT-time: 09:11 (Saturday, 18-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat