Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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.
I can run it while in the /root/ directory and it works fine, if I CD out and try to run it, it doesn't work. If I copy the script into the actual root "/" folder it doesn't work. It only works in the /root/ folder.
I think the problem is in the highlighted line, but I'm not sure and I have very little knowledge of BASH and the commands being used. (if only I could do it in PHP )
I have tried to create the script in other folders, but it only runs in root. I have no idea why.
Any help would be great.
Last edited by BenPhelps; 08-03-2009 at 03:23 PM..
Reason: iptables root folder run
I can't see anything in the bash itself that would explain what you are seeing but I am not familiar with vzlist and iptables. The problem you describe is unusual for bash script -- bash doesn't care where the script is just so long as you give a pathname that it can find. Are you sure it is not your current working directory that is the problem? Have you tried running the command /root/vz-generate-traffic-log from different directories including an empty one?
Might be interesting to post the output -- it's a very long-winded way of parsing something! That [:blank:] would be safer in single quotes if you want to pass it verbatim to tr.
Please confirm you are running this as root (as suggested by your command prompt). What exactly do you mean by "doesn't work"? Any error messages? No output?
Here is me showing what happens when its ran outside of its residing DIR.
Code:
login as: root
root@67.215.240.178's password:
Linux vpserv 2.6.24-7-pve #1 SMP PREEMPT Tue Jun 2 08:00:29 CEST 2009 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Aug 3 10:36:32 2009 from r74-195-216-42.msk1cmtc02.mskgok.ok.dh.suddenlink.net
vpserv:~# ls
vz-generate-traffic-log
vpserv:~# ./vz-generate-traffic-log
vz-traffic.log 100% 984 1.0KB/s 00:00
vpserv:~# ./vz-generate-traffic-log
vz-traffic.log 100% 893 0.9KB/s 00:00
vpserv:~# cd ..
vpserv:/# /root/vz-generate-traffic-log
vz-traffic.log 100% 851 0.8KB/s 00:00
vpserv:/# /root/vz-generate-traffic-log
vz-traffic.log 100% 851 0.8KB/s 00:00
vpserv:/# cp /root/vz-generate-traffic-log vz-generate-traffic-log
vpserv:/# ./vz-generate-traffic-log
vz-traffic.log 100% 851 0.8KB/s 00:00
vpserv:/# sh vz-generate-traffic-log
vz-traffic.log 100% 851 0.8KB/s 00:00
vpserv:/# sudo sh vz-generate-traffic-log
vz-traffic.log 100% 851 0.8KB/s 00:00
vpserv:/# mkdir test
vpserv:/# cp /root/vz-generate-traffic-log /test/vz-generate-traffic-log
vpserv:/# cd test
vpserv:/test# ./vz-generate-traffic-log
vz-traffic.log 100% 926 0.9KB/s 00:00
vpserv:/test# cd ..
vpserv:/# /test/vz-generate-traffic-log
vz-traffic.log 100% 851 0.8KB/s 00:00
vpserv:/# whoami
root
vpserv:/#
I cannot run the script from crontab, it just doesn't work. Investigating, I found that it was this problem.
Any transfer of 851 bytes is incorrect an shows that the script failed.
Last edited by BenPhelps; 08-03-2009 at 05:24 PM..
It looks like bash always runs the script OK; the problem is that the output from the commands is not what you expect. Which command produces the output you show? How do you know it is not correct?
About the output, the part of the script that is not working is the iptables line in the loop. It outputs 0 when it should output a much larger number (traffic transfer in bytes). When it outputs 0 and not the correct number, the file size is always 851 bytes. By viewing the file it transfers, if the file size is 851 bytes, its the file with the formatting and no data (all traffic shows as 0). When its more than 851 bytes, its the file with data and formatting.
I would show the output but its full of client IP addresses.
I made a temporary fix using this shell script.
Code:
#!/bin/bash
cd /root/
./vz-generate-traffic-log
The script can be run when started by that (while working directory is in or out of its residing directory), but it still doesn't explain why the script is having problems in the first place.
Last edited by BenPhelps; 08-04-2009 at 12:04 PM..
First of all, if you would want to use your script in crontab, you would ALWAYS have to specify full paths to ALL commands (e.g. /sbin/iptables -nvz ...) because cron default environment does not provide this.
Secondly, your script needs to have correct attributes (e.g. chmod 775)
Also which command interpreter are you using in your script? (i.e. first line of your script) is it #!/bin/sh or #!/bin/bash
About the output, the part of the script that is not working is the iptables line in the loop. It outputs 0 when it should output a much larger number (traffic transfer in bytes). When it outputs 0 and not the correct number, the file size is always 851 bytes. By viewing the file it transfers, if the file size is 851 bytes, its the file with the formatting and no data (all traffic shows as 0). When its more than 851 bytes, its the file with data and formatting.
I would show the output but its full of client IP addresses.
I made a temporary fix using this shell script.
Code:
#!/bin/bash
cd /root/
./vz-generate-traffic-log
The script can be run when started by that (while working directory is in or out of its residing directory), but it still doesn't explain why the script is having problems in the first place.
The script is the forest; the iptables line in it is the tree. Either the iptables output is wrong or the line that parses its output is wrong. It is unlikely that iptables output is wrong because it is a widely used command. This leaves the parsing of the iptables output.
Can you post the output of the successive stages of the iptables output, editing out the confidential data? I mean the output of
Either you can review it yourself, to see if each stage is doing what it is intended to do or you could edit the confidential data, changing the IP addresses to xxx.xxx.xxx.xxx. We only need to see what happens to a single representative sample line so that need not be onerous.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.