LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-26-2016, 08:58 AM   #1
QuestionMqrk
LQ Newbie
 
Registered: Oct 2016
Posts: 29

Rep: Reputation: Disabled
How to create a script out of a cron job?


Hi

I have an enormous migraine right now and need to know how to turn this

Code:
0 11 * * * /bin/date >> /home/michaelzheludev/CRON/smartctl-output && /usr/sbin/smartctl -aixcA -d sat /dev/sda | /bin/grep -w "Reallocated_Sector_Ct\|Spin_Retry_Count\|Reallocated_Event_Count\|Current_Pending_Sector\|Offline_Uncorrectable" >>  /home/michaelzheludev/CRON/smartctl-output
into a script. I have zero knowledge of scripts and my headache just wont let me focus on anything, much less reading complicated tutorials. I need to have this done asap... Please help.
 
Old 10-26-2016, 09:26 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
You can make a script simply by putting a command in a file then making it executable:

e.g.
Run "vi test.sh" to start editing a file with the name "test.sh".
Once in the vi (vim) session just do "i" to insert and type "ls -l"
Hit escape then do ":wq!" to save the file and exit.
Type "chmod +x test.sh" to make it executable.

Run "./test.sh" and you'll see the "ls -l" output.

So your cron entry is:
0 11 * * * /bin/date >> /home/michaelzheludev/CRON/smartctl-output && /usr/sbin/smartctl -aixcA -d sat /dev/sda | /bin/grep -w "Reallocated_Sector_Ct\|Spin_Retry_Count\|Reallocated_Event_Count\|Current_Pending_Sector\|Offline_U ncorrectable" >> /home/michaelzheludev/CRON/smartctl-output

The first part of that "0 11 * * *" is the time specification for when cron will run it.
Does this cron job actually run as written? If so you can just add the rest of the line without the time specification to a file as you did in the test.sh above.

You can have a look at man pages for details on various things e.g.
man cron
man crontab
man date
man grep
man bash (The scripts are generally by default bash scripts on Linux.)

By the way your script name can be anything - it does not have to end with .sh as Linux, unlike DOS/Windows, doesn't require suffixes - these are just additional characters so you could call your script just "test" or "bob" or anything. The ".sh" is just an easy convention to let you and others know later that it was intended to be a shell script. Similarly you'll often see ".pl" on perl scripts and other names for other scripting languages.

Also it is usual to put an interpreter line at the beginning of the script to invoke the specific scripting/interpreter you expect. You do this by having the first line start with "shebang" which is "#!" then the intepreter such as /bin/bash for bash shell script so it is "#!/bin/bash". If you're actually in a bash shell (which again is the default for most Linux logins) the shebang isn't required but it is a good practice to do it even so.

If you do a web search for "bash scripting tutorial" you'll find several links that will help you get started.
 
Old 10-26-2016, 09:28 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
http://www.linuxquestions.org/questi...eniuses-35795/

Get some! asap

Last edited by Habitual; 10-26-2016 at 09:32 AM.
 
Old 10-26-2016, 12:18 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
Blog Entries: 4

Rep: Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894
A basic template is the following:

Code:
#!/bin/sh

set -evx

# your script with comments ...

exit ( 0 );
Once it is working, you can remove the set -v -x and leave the set -e. See the man page for "sh" for details on set on the others but the -e will cause the script to quit if it encounters any errors that are not planned for.

You can leave in the various && operators if you like, but in some cases replacing them with if-then or if-then-else will be more readable.

If you are wondering where to store the script once you have it working, you can put it in /usr/local/sbin/ if it is a local system administration script.
 
Old 10-27-2016, 08:13 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,529

Rep: Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593Reputation: 1593
Quote:
Originally Posted by QuestionMqrk View Post
Hi

I have an enormous migraine right now and need to know how to turn this

Code:
0 11 * * * /bin/date >> /home/michaelzheludev/CRON/smartctl-output && /usr/sbin/smartctl -aixcA -d sat /dev/sda | /bin/grep -w "Reallocated_Sector_Ct\|Spin_Retry_Count\|Reallocated_Event_Count\|Current_Pending_Sector\|Offline_Uncorrectable" >>  /home/michaelzheludev/CRON/smartctl-output
into a script. I have zero knowledge of scripts and my headache just wont let me focus on anything, much less reading complicated tutorials. I need to have this done asap... Please help.
I warned you that you'd have to turn it in to a script eventually I actually had the whole thing written as a script but didn't post it at the time.
 
Old 10-27-2016, 03:09 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 2,985

Rep: Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273
You can shorten it a bit.
In crontab
Code:
0 11 * * * { /bin/date; /usr/sbin/smartctl -aixcA -d sat /dev/sda | /bin/grep -w "Reallocated_Sector_Ct\|Spin_Retry_Count\|Reallocated_Event_Count\|Current_Pending_Sector\|Offline_Uncorrectable"; } >> /home/michaelzheludev/CRON/smartctl-output
In a shell script, say you named it "myscript"
Code:
#!/bin/sh
{
/bin/date
/usr/sbin/smartctl -aixcA -d sat /dev/sda |
/bin/grep -w "Reallocated_Sector_Ct\|Spin_Retry_Count\|Reallocated_Event_Count\|Current_Pending_Sector\|Offline_Uncorrectable"
} >> /home/michaelzheludev/CRON/smartctl-output
and in crontab run the executable shell script with
Code:
0 11 * * * /path/to/myscript
 
1 members found this post helpful.
  


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
How do I create a cron job that will run everyday at 12:20am? and 5:20 AM ? wireshark11 Linux - Newbie 8 01-29-2014 09:08 PM
cron job to create snapshots not working spoovy Solaris / OpenSolaris 4 11-15-2011 05:24 AM
How to create cron job to execute at same time each day? damgar Slackware 4 11-19-2010 07:27 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 09:16 AM
How to create a cron job to delete a folder's contents? s0n|k Linux - Newbie 1 10-05-2007 08:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:57 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
Open Source Consulting | Domain Registration