Domanda

i am trying to perform a SQL file importation into my mysqlDB, well this should be happening every 5 mints, therefore i used crontab -e:

*/5 * * * * /usr/bin/mysql -u root -pexpress azuzDB < /home/admin/Desktop/backups/backup_n1.sql

well the cron job works fine, but in a matter of issues the backup_n*.sql name changes every 5 mints also, and i am interested in the latest sql file in the folder so i came with idea to get the latest file by applying this command:

ls /home/admin/Desktop/backups -Art | tail -n 1

so i would like to pass this command output which is = (backup_n*.sql "most recent")

in the importation cron job command i use in crontab any solution to pass variables in a crontab of centos ??

the last output would be something like this:

 * * * * * lastSQL= ls /home/admin/Desktop/backups -Art | tail -n 1
 */5 * * * * /usr/bin/mysql -u root -pexpress azuzDB < /home/admin/Desktop/backups/$lastSQL
È stato utile?

Soluzione 2

As indicated in comments, the way to get the last file can be:

ls -Art | tail -n 1

If you wrap it within $(), it will be evaluated whenever you place it. Hence, you can do:

/usr/bin/mysql -u root -pexpress azuzDB < /home/admin/Desktop/backups/$(ls -Art /home/admin/Desktop/backups -Art | tail -n 1)

Altri suggerimenti

I suggest you to create a shell script, say called /home/admin/import.sh

$ cat import.sh 
#! /bin/bash
dir=/home/admin/Desktop/backups/
file=$( ls -At  $dir )
/usr/bin/mysql -u root -pexpress azuzDB < ${dir}/${file}

and have your crontab to look like:

*/5 * * * * /bin/bash /home/admin/import.sh
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top