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 |
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. |
|
A basic template is the following:
Code:
#!/bin/sh 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. |
Quote:
|
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 Code:
#!/bin/sh Code:
0 11 * * * /path/to/myscript |
All times are GMT -5. The time now is 05:45 AM. |