Linux - SoftwareThis 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.
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.
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.
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.
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?
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
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.
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.
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.
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');"
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.