Automate a .sh script

3

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

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

To copy and paste files from one folder to 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 two days ago (since I do not have data available for the last 24h)

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 do not have to manually update this date.

I need the script in addition to updating the date automatically, also rotate at a certain time of day and I get an error message if I can 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 that.

Here 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}/
    
asked by anonymous 20.11.2017 / 17:13

1 answer

4

Come on ...

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

DATA

Using this concatenated code, the string where the date is inserted will fetch the date of the current day.

date +%d/%m/%Y

AUTOMATION

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

For scheduling to work, you must follow a pattern, a format to which you must adhere. 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

Oh yeah, well, it's also possible to get a date before or after 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 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 such as this:

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

The script.sh will run every hour.

UPDATE # 3

Follow the code modified by me but not 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

Correction in code.

UPDATE # 5

Correction of the cron chart illustration.

Good luck!

    
20.11.2017 / 17:24