Automate a script . sh

Asked

Viewed 1,092 times

3

I have a Shell Script (.sh) and need to automate it. Could someone help me??

This script copies data from a folder and pastes (converting to another format) into another directory, runs a Python script for plotting data, and updates the information obtained on a web page.

To copy and paste files from one folder into another, the script reads the file name, which contains the date entered, and executes the other commands from there.

The data to be copied is from two days ago (since I do not have available the data of the last 24 hours)

The problem is that I need to manually change the date, every day.

I need to find a way to automate this process so that I don’t have to manually update the date.

I need the script to update the date automatically, also run at a certain time of day and return me an error message if I do not find data in the folder to be copied.

I’m new to programming and so I’m having a hard time finding the best way to do this.

Below is the code I’m using:

!/bin/bash

date="2017-11-17"

year=`date +"%Y"`
previous_year=`date +"%Y" -d "-1 year"`

h4=/mnt/raid/CALIPSO/SCRIPTS/

dir=/mnt/raid/CALIPSO/DATA/NETCDF_TEMP/ 

cd /mnt/raid/CALIPSO/DATA/L1.5/2017/

cp CAL_LID_L15_Exp-Beta-V3-40.${date}T*.hdf  /mnt/raid/CALIPSO/DATA/NETCDF_TEMP

for i in ${dir}*.hdf; do ${h4}h4tonccf_nc4 $i; done

python ${h4}CalipsoLatLonTimeLoop_TimTrack.py

#Uploading the data to WEB server
rsync -u -z -v -e "ssh -p 8222" /mnt/raid/CALIPSO/PICS/${year}* [email protected]:/home/www/html/rt/PICS/${year}/
rsync -u -z -v -e "ssh -p 8222" /mnt/raid/CALIPSO/PICS/${previous_year}* [email protected]:/home/www/html/rt/PICS/${previous_year}/
  • Eventually get a better name for the script (a script called script is like a dog named dog)

1 answer

4


Come on...

Well, I’ll try to help solve these obstacles but if I had posted the code it would be easier...

DATE

Using this code concatenated the string where the date is inserted will take the current day date.

date +%d/%m/%Y

AUTOMATION

To automate the execution of this script just use cron. To add a task to cron you need to open it with any text editor (if you are using a Gnome graphical interface, and if you have installed it, you can use gedit, or if vim ,vim, nano, pipe as you prefer) or file /etc/crontab and schedule, setting the month/day/time when the command should be executed. For the cron tool to work it is not necessary to restart it.

For scheduling to work it is necessary to follow a pattern, a format to which you must respect. See the example below:

[minutos] [horas] [dias do mês] [mês] [dias da semana] [usuário] [comando]


31 18 1 * * root run-parts --report /etc/cron.montly
|   | | | |   |    |
|   | | | |   |     \_Comando que será executado
|   | | | |   |
|   | | | |    \_ UID que executará o comando
|   | | | |
|   | | | \_ Dia da semana (0-7)
|   | | |
|   | |  \_ Mês (1-12)
|   | |
|   |  \_ Dia do Mês (1-31)
|   |
|   \_ Hora
|
\_ Minuto

UPDATE #1

Ah understood, well it is also possible to pick a date earlier or later than the current day. Follow this example:

ontem=$(date --date="yesterday" +"%d/%m/%Y")
echo "O backup foi realizado $ontem"

UPDATE #2

No problem, I’m here to help you. cron is an operating system task scheduler, you should put the execution of your script as a task of it. The easiest way to edit cron is:

sudo crontab -e

When adding a line like this for example:

* 1 * * * root /caminho/do/seu/script.sh

The "script.sh" will be executed every hour.

UPDATE #3

Follow the code modified by me though nay tested.

!/bin/bash

ontem=$(date --date="2 days ago" +"%Y-%m-%d");

date=$ontem

year=date +"%Y" previous_year=date +"%Y" -d "-1 year"
h4=/mnt/raid/CALIPSO/SCRIPTS/
dir=/mnt/raid/CALIPSO/DATA/NETCDF_TEMP/
cd /mnt/raid/CALIPSO/DATA/L1.5/$year/
cp CAL_LID_L15_Exp-Beta-V3-40.${date}T*.hdf /mnt/raid/CALIPSO/DATA/NETCDF_TEMP

for i in ${dir}*.hdf; do ${h4}h4tonccf_nc4 $i; done

python ${h4}CalipsoLatLonTimeLoop_TimTrack.py

UPDATE #4

Code correction.

UPDATE#5

Cron table illustration correction.

Good Luck!

  • Hi.. Thanks for the help, but I’m still having some questions: Is the command you gave (date +%d/%m/%Y) enough? I did not explain the first time, I’m sorry, but the data to be copied and pasted is not of the current day but yes, two days ago. Therefore, today for example I run this script for the data of the day 18. The data of the day 19 and today (day 20) are not yet available. Therefore, I believe, that another command needs to be executed always taking into account the date of two previous days, no? I’ll update the post with the code I’m using

  • One more thing, regarding cron, wouldn’t this command that you passed enter this script then? Would it be run separately? Sorry for the broken explanations, as I said I’m new to this aera and I’m having a hard time understanding how things work...and thanks for the help!

  • I gave a complimentary, take a look.... If you need more help just post here or call me at the luigibredaa Inst

  • So instead of the date="2017-11-17", I put: yesterday=$(date --date="Yesterday" +"%d/%m/%Y") echo "The backup was done $yesterday" In this case, the date --date, already referring to the date two days ago? Or do I need to specify it another way? As for cron, now I get it! Thank you! =)

  • I’m glad you understood the cron question... so I tweaked your code, can you test it and let me know? Don’t forget to evaluate my answer.

  • I tested and it didn’t work...from what I understood of the error, the program is trying to get the data of yesterday (which are not yet available) and not two days ago... PS: response evaluated!

  • Thank you, just change it to --date="2 days ago"

  • Thanks! but still giving error: cp: cannot stat `Cal_lid_l15_exp-Beta-V3-40.2017/11/18T*. hdf': No such file or directory Fail to open the HDF4 file with Hopen.(hdf4_file.cpp:56) Error to call h4cf_open. The program exits abnormally

  • The error you gave is because it failed to copy the file it does not exist. Check this.. the date issue worked however the files are with name different from the day before yesterday apparently.

  • When I run the old code, with the date="2017-11-17", it will...are the same directories and the same files, so I’m not understanding this error. File name does not change, soh changes date

  • I already know... the date we are creating automatically is separated by / and yours is by -. I will update the post.

  • It worked!!! Thank you very much! =)

  • That’s not why, if you need to call me at the Insta. @luigibredaa

  • I think there are two more spaces in the arrows of your drawings. Minutes is right, but everything that comes after should be two characters to the left. It caused a little confusion the first time I went to read, because Dia da Semana is pointing to the UID, and what is being pointed out by UID is pointing to the command to be executed

  • Edited, thank you Jefferson Quesado.

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.