LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to create a script out of a cron job? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-create-a-script-out-of-a-cron-job-4175592263/)

QuestionMqrk 10-26-2016 07:58 AM

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.

MensaWater 10-26-2016 08:26 AM

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.

Habitual 10-26-2016 08:28 AM

http://www.linuxquestions.org/questi...eniuses-35795/

Get some! asap

Turbocapitalist 10-26-2016 11:18 AM

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.

TenTenths 10-27-2016 07:13 AM

Quote:

Originally Posted by QuestionMqrk (Post 5623171)
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.

MadeInGermany 10-27-2016 02:09 PM

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


All times are GMT -5. The time now is 05:45 AM.