Friday, November 27, 2015

My bash script doesn't work in crontab

........Continuing to bash script yesterday..

I got a problem where it doesn't work when it is running using the crontab.

Here is some tips:
1. You can troubleshoot by keep the log of your cron
eg: 10 02 * * * /home/aminah/script.sh  >> /home/aminah/cronscripts.log 2>&1

So, you can tail the cronscripts.log once it is running.

2.  Path of you script.
Make sure you put the full path so that the script will understand where you put the script.

3. If your script is download the files, it will download it at home of your running bash file.
eg: run as root.. the download file will located at /root/


It is really good experience for me... Thanks to Allah swt....

Wednesday, November 25, 2015

Playing with bash file

This is my first command of bash file.. Usually i'm only adding few command which is already there (who already wrote by my SIFU)..

What I'm going to do is.. transfering file from sftp server A to server B and send to S3 amazon. The tricky is sftp part.. :) .. I'm creating 2 file here sftpcommand.txt and script.sh


sftpcommand.txt
+++++++++++++++++++
mget data-2015_11_24*
bye

+++++++++++++++++++


script.sh
++++++++
#!/bin/bash
now="$(date)"         
printf "Current date and time %s\n" "$now"      #date of the day     

now="$(date +'%Y_%m_%d')"
printf "Current date in yyyy_mm_dd format %s\n" "$now"   

#.......
echo "try command"
#echo "data-$(date -d "3 day ago" +'%Y_%m_%d')"
now="data-$(date -d "1 day ago" +'%Y_%m_%d')*"   #1day before current date with wildcard*

echo "startfilename=""$now"  #show the output of $now

echo "changing sftp command"
sed -i "1s/.*/mget $now/" sftpcommand.txt    #changing sftpcommand.txt at 1st line same value with $now

echo "sftp is running"
sftp -b sftpcommand.txt -v -P 9878 -i cthroot.pem root@sftp.server.com:   #sftp bash command of sftpcommand.txt

echo "list"
find /home/aminah/data/ -name "$now" -print  #work
echo "send to s3"
find /home/aminah/data/ -name "$now" -exec aws s3 cp '{}' s3://pathbucket/folder/ \;   #work #sending the same value to s3.. 

echo "end"
+++++++

To check the command run bash  #bash -x script.sh ; it will show the output and easy to troubleshoot. 

And its working like charm.. horey...