Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-07-2014, 03:58 PM
|
#1
|
Member
Registered: Jan 2012
Posts: 72
Rep:
|
Script help cron
Dear All,
I wrote a bash script which reads values and put in DB. I am facing issue with value of variable "Server_ip" while running this script via cron job.When I execute this script manually, it inserts all values in DB correctly. But when I try to run this script via cron job it misses Server_ip value inserting in DB.
Script is as follows:
##########################
#/!bin/bash
Server_ip=$(ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}')
Current_Year=$(date +'%Y')
Today_Date=$(date | cut -b5-10)
Yesterday_Date=$(date --date="1 days ago" | cut -b5-10)
#last | grep "$Today_Date" > /home/kamran/ssh_logins.txt
last | grep "$Yesterday_Date" > /home/kamran/ssh_logins.txt
cat /home/kamran/ssh_logins.txt | grep . | grep -v reboot | grep -v shutdown |grep -v wtmp | grep -v tty | awk '{print $1","$2"," $3 "," $4 "-" $5 "-" $6 "," $7 "," $9}' > /home/kamran/output.csv
IFS=,
while read Login_Name Login_Terminal Login_IP Login_Date Login_Start_Time Login_End_Time
do
echo "INSERT INTO ssh_logins (Login_Name,Login_Terminal,Login_IP,Login_Date,Login_Start_Time,Login_End_Time,Server_ip,Current_Yea r) VALUES('$Login_Name','$Login_Terminal','$Login_IP','$Login_Date','$Login_Start_Time','$Login_End_Tim e','$Server_ip','$Current_Year');"
done < /home/kamran/output.csv | mysql -hlocalhost -uroot -ppassword ssh_logins;
#################
Cron entry looks like as follows:
00 01 * * * /home/kamran/ssh-logins1.sh > /dev/null 2>&1.
I dont know why it is missing to insert Server_ip variable while running with cron.
Please help.
Regards.
Kamran
Last edited by kamran.ayub; 11-07-2014 at 04:03 PM.
Reason: spell mistake correction
|
|
|
11-07-2014, 04:17 PM
|
#2
|
Member
Registered: Apr 2012
Distribution: Gentoo
Posts: 616
Rep:
|
Did you check
ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'
output? Not from script, just from terminal?
For me it shows nothing, empty string.
AFAIK your command will lead to empty Server_ip.
Code:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255
ether d0:27:88:7d:e8:c6 txqueuelen 1000 (Ethernet)
RX packets 24620 bytes 17182840 (16.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 25184 bytes 5142742 (4.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Its my output for "ifconfig eth0".
Note that there is no "inet addr" substring in ifconfig output, so grep will not pipe any substring to awk.
Maybe you have to change grep "inet addr" to grep "inet" and correct parameters for awk?
|
|
|
11-07-2014, 04:21 PM
|
#3
|
Member
Registered: Jan 2012
Posts: 72
Original Poster
Rep:
|
Dear Teufel,
Following is my output :
[root@test ~]# echo Server_ip=$(ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}')
Server_ip=10.111.5.215
Output of ifconfig eth0 is :
[root@test ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:11:85:BB:FF:0A
inet addr:10.111.5.215 Bcast:10.111.5.255 Mask:255.255.255.0
inet6 addr: fe80::211:85ff:febb:ff0a/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:19839036 errors:0 dropped:0 overruns:0 frame:0
TX packets:141193 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2173800523 (2.0 GiB) TX bytes:32200612 (30.7 MiB)
Interrupt:49
Thanks
regards,
Kamran
|
|
|
11-07-2014, 04:25 PM
|
#4
|
Member
Registered: Apr 2012
Distribution: Gentoo
Posts: 616
Rep:
|
May be your server's output differs?
OK, you could temporary set you variable Server_ip like this:
Server_ip=$(ifconfig eth0)
It will fill your database field with full ifconfig output. And you could read it from database to get idea, what a real value will be passed to DB by ifconfig.
P.S.
DB field for Server_ip must be enough length to fit all ifconfig output
Last edited by Teufel; 11-07-2014 at 04:30 PM.
|
|
|
11-07-2014, 04:34 PM
|
#5
|
Member
Registered: Jan 2012
Posts: 72
Original Poster
Rep:
|
Dear Teufel,
Same effect. When I run script manually after replacing server_ip variable value it fills in DB column value.
But when same script tries with cron job it has not fill any value in column Server_ip.
Don't know what is effecting the script to run via cron job in this scenario.
Regards,
kamran
|
|
|
11-07-2014, 04:52 PM
|
#6
|
Member
Registered: Apr 2012
Distribution: Gentoo
Posts: 616
Rep:
|
Can you redirect to text file (log) your INSERT statement to look how exactly INSERT query looks like?
|
|
|
11-07-2014, 04:59 PM
|
#7
|
Member
Registered: Jan 2012
Posts: 72
Original Poster
Rep:
|
Dear Teufel,
Following is the output for redirecting my insert query to text file.
[root@test ~]# cat /home/kamran/output.txt
INSERT INTO ssh_logins (Login_Name,Login_Terminal,Login_IP,Login_Date,Login_Start_Time,Login_End_Time,Server_ip,Current_Yea r) VALUES('kamran','pts/0','10.111.11.135','Fri-Nov-7','00:34','05:01','10.111.5.215','2014');
But main issue I am facing is running this script via cron job.When I run script manually it exactly dies what I want and what it is showing in above output. But when I run via cron job it misses server_ip field.
regards,
Kamran
|
|
|
11-07-2014, 05:22 PM
|
#8
|
Member
Registered: Apr 2012
Distribution: Gentoo
Posts: 616
Rep:
|
It's strange enough
The only suspicious thing I see in your script is that your script should start with this line:
not
Code:
###################
#!/bin/bash
Sometimes blank strings (or strings like #########) may lead to unpredictable errors.
It's just shot in the dark, but nevertheless, change it to correct #!/bin/bash in very first line.
Quote:
Originally Posted by Bash Guide for Beginners
The first line of the script determines the shell to start. The first two characters of the first line should be #!, then follows the path to the shell that should interpret the commands that follow. Blank lines are also considered to be lines, so don't start your script with an empty line.
|
|
|
|
11-07-2014, 05:34 PM
|
#9
|
Member
Registered: Jan 2012
Posts: 72
Original Poster
Rep:
|
Dear Teufel,
My script starts with #!/bin/bash.
Those ######## signs I put here are just my marker line for reading script start here in thread.
Script does not originally contains these signs.
Thanks for your efforts.
Anybody else who can help in this regard please. I dont know what is doing wrong when executing script via cron job.
Regards,
Kamran
|
|
|
11-07-2014, 06:46 PM
|
#10
|
Member
Registered: Apr 2012
Distribution: Gentoo
Posts: 616
Rep:
|
OK, I installed cron just out of curiosity, whats the matter and run this script:
Code:
#!/bin/bash
Server_ip=$(ifconfig eth0 | grep "inet ")
Current_Year=$(date +'%Y')
Today_Date=$(date | cut -b5-10)
Yesterday_Date=$(date --date="1 days ago" | cut -b5-10)
echo "INSERT INTO ssh_logins (Login_Name,Login_Terminal,Login_IP,Login_Date,Login_Start_Time,Login_End_Time,Server_ip,Current_Yea r) VALUES('$Login_Name','$Login_Terminal','$Login_IP','$Login_Date','$Login_Start_Time','$Login_End_Tim e','$Server_ip','$Current_Year');" >> ~/testfile
Note that INSERT statement exactly the same as you posted above, I just copypasta it from your first message.
So, what I got on cron activation in testfile:
Code:
INSERT INTO ssh_logins (Login_Name,Login_Terminal,Login_IP,Login_Date,Login_Start_Time,Login_End_Time,Server_ip,Current_Yea r) VALUES('','','','','',' e','','2014')
Note that ' e' field before Server_ip place.
After that I changed your INSERT statement like this:
Code:
echo "INSERT INTO ssh_logins (Login_Name,Login_Terminal,Login_IP,Login_Date,Login_Start_Time,Login_End_Time,Server_ip,Current_Yea r) VALUES('$Login_Name','$Login_Terminal','$Login_IP','$Login_Date','$Login_Start_Time','$Login_End_Time','$Server_ip','$Current_Year');"
and got this string in testfile:
Code:
INSERT INTO ssh_logins (Login_Name,Login_Terminal,Login_IP,Login_Date,Login_Start_Time,Login_End_Time,Server_ip,Current_Yea r) VALUES('','','','','','','inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255','2014')
It works for me even by running via cron.
Probably you have to check you script letter-by-letter once more to prevent random syntax errors, unnecessary white spaces, CRLFs e.t.c
And please wrap your code with tag. It helps to read code.
Last edited by Teufel; 11-07-2014 at 06:47 PM.
|
|
|
11-08-2014, 03:16 AM
|
#11
|
Member
Registered: Jan 2012
Posts: 72
Original Poster
Rep:
|
Dear Teufel,
That e is basically end letter of last field "Login_End_Time".
I have a workaround for this issue by hardcoding server ip in script just like
Server_ip=x.x.x.x.x
And in this way it got correct insert values even by runnning script via cron.
I will give more time to this to sort it out that why not working with cron when picking variable value from awk&ifconfig.
Thanks for giving your time.
Regards,
Kamran
|
|
|
All times are GMT -5. The time now is 10:53 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|