LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 11-07-2014, 02:58 PM   #1
kamran.ayub
Member
 
Registered: Jan 2012
Posts: 72

Rep: Reputation: Disabled
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 03:03 PM. Reason: spell mistake correction
 
Old 11-07-2014, 03:17 PM   #2
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
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?
 
Old 11-07-2014, 03:21 PM   #3
kamran.ayub
Member
 
Registered: Jan 2012
Posts: 72

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-07-2014, 03:25 PM   #4
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
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 03:30 PM.
 
Old 11-07-2014, 03:34 PM   #5
kamran.ayub
Member
 
Registered: Jan 2012
Posts: 72

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-07-2014, 03:52 PM   #6
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
Can you redirect to text file (log) your INSERT statement to look how exactly INSERT query looks like?
 
Old 11-07-2014, 03:59 PM   #7
kamran.ayub
Member
 
Registered: Jan 2012
Posts: 72

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-07-2014, 04:22 PM   #8
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
It's strange enough
The only suspicious thing I see in your script is that your script should start with this line:
Code:
#!/bin/bash
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.
 
Old 11-07-2014, 04:34 PM   #9
kamran.ayub
Member
 
Registered: Jan 2012
Posts: 72

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-07-2014, 05:46 PM   #10
Teufel
Member
 
Registered: Apr 2012
Distribution: Gentoo
Posts: 616

Rep: Reputation: 142Reputation: 142
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 05:47 PM.
 
Old 11-08-2014, 02:16 AM   #11
kamran.ayub
Member
 
Registered: Jan 2012
Posts: 72

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script runs Manually but not in Cron, yet other parts of the script run mccartjd Linux - Newbie 5 01-08-2012 06:54 PM
Cron appears to be messed up (E: cron: subprocess post-installation script returned e bujutsukai Linux - Newbie 1 07-24-2008 03:39 AM
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 08:09 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
PPPD Script problem when cron-scheduling the script andresurzagasti Linux - Networking 0 11-24-2004 02:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration