Do you know why this loop returns directory listing results??
#!/bin/bash
/usr/sbin/httpd fullstatus | while read line
do
echo $line
done
71-0 - 0/0/410 . 7.74 47987 0 0.0 0.00 0.76 127.0.0.1
OPTIONS = bin boot dev error_log etc home lib lib64 lost+found media mnt nohup.out opt proc root sbin selinux srv sys test tmp usr var HTTP/1.0
72-0 - 0/0/103 . 0.14 48912 0 0.0 0.00 0.13 127.0.0.1
OPTIONS = bin boot dev error_log etc home lib lib64 lost+found media mnt nohup.out opt proc root sbin selinux srv sys test tmp usr var HTTP/1.0
It should returns only apache status.
71-0 - 0/0/410 . 7.74 48231 0 0.0 0.00 0.76 127.0.0.1
OPTIONS * HTTP/1.0
72-0 - 0/0/103 . 0.14 49157 0 0.0 0.00 0.13 127.0.0.1
OPTIONS * HTTP/1.0
Thanks
Because of this line on output
OPTIONS * HTTP/1.0
shell expands "*" used in "echo" arguments as list of files in current directory. This script's output will differ depending on $CWD of calling shell.
Go to any chosen directory, and type "echo *" command.
Because * gets interpreted.
Remember:
ALWAYS QUOTE YOUR VARIABLES
In this case:
echo "$line"
Piotr already answered the question. Just some addition. I would suggest not to pipe anything to while! It will create another bash process which is wasting resources and You will face to problems if you define a variable inside the while loop and you want to use it outside the loop. I may suggest to use some other solution, like:
#!/bin/bash
while read line; do
echo "$line"
done < <(/usr/sbin/httpd fullstatus)
Related
I'm trying to use the phanton-js plugin for Nagios:
https://github.com/hggh/phantomjs-nagios
but despite me double checking the set-up, I'm getting
"Return code 127 is out of bounds : Plugin may be missing"
The plugin file exists in the plugins folder:
# stat /usr/lib64/nagios/plugins/check_http_load_time.rb
File: `/usr/lib64/nagios/plugins/check_http_load_time.rb'
Size: 9108 Blocks: 24 IO Block: 4096 regular file
Device: fc01h/64513d Inode: 275201 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
I can execute it when su'd to the nagios user locally:
-bash-4.1$ whoami
nagios
-bash-4.1$ /usr/lib64/nagios/plugins/phantomjs-nagios/check_http_load_time.rb -u https://google.com -w 2 -c 3
OK: https://google.com load time: 0.43
This is how it is defined in the checkcommands.cfg file:
define command {
command_name check_web_page_load_time
command_line $USER1$/check_http_load_time.rb -u $ARG1$ -w $ARG2$ -c $ARG3$
}
and this is the service definition
define service {
use generic-service
host_name test_host
service_description https://google.com web load time
check_command check_web_page_load_time!https://google.com!2!3
contact_groups support-emails
}
I've restarted nagios and confirmed that the config check passes.
Am I missing something obvious?
edit:
plugin file is located in 2 folders:
/usr/lib64/nagios/plugins/check_http_load_time.rb
and
/usr/lib64/nagios/plugins/phantomjs-nagios/check_http_load_time.rb
both files are identical, both can be executed by the nagios user successfully and neither location works with command definition. I tried calling it with full path and with the $USR1$ variable, which points to the standard location where all the other plugins are located
/usr/lib64/nagios/plugins/
Manged to eventually solve it. The error message was not very helpful, as problem was with the environment variables of the nagios user.
The original script had the following shebang:
#!/usr/bin/env ruby
After changing it to the actual location of ruby binary the script works:
#!/usr/local/rvm/rubies/ruby-2.5.1/bin/ruby
Still surprised that it worked when su'd to the Nagios user.
I have few thousands of folders which store some files and (the folders) are named in the following fashion:
0.01
0.02
.
.
.
1.01
.
.
.
Normally, I would use cp -r {1..1000} some/destination, however trying to do cp -r {0.01..0.21} some/destination does not work.
Also, if I would want to copy only the every fifth folder?
0.05
0.1
0.15
.
.
.
Once again, files would be in an array, and would end at a specific number, for instance 1.2.
Make use of the seq command:
seq [OPTION]... FIRST INCREMENT LAST
-f, --format=FORMAT use printf style floating-point FORMAT
source: man seq
Thus for all folders:
cp -r $(seq 0.01 0.01 0.21) some/destination
If you want to copy only every 5th folder:
cp -r $(seq 0.05 0.05 0.21) some/destination
However, this seq will create a list of folder-names with names like 2.00. If you do not want the trailing zeros, you need to reformat it a bit by adding the flag -f '%g'
Probably you are better off using an array so that you are resilient to the issue of folders not following a strict sequence, though this is not as efficient as copying multiple directories with each invocation of cp as in kvantour's answer:
dirs=(*.*/) # get list of directories into an array
n=0
for dir in "${dirs[#]}"; do # traverse the array
(( ++n % 5 == 0 )) || continue # skip if it is not the 5th
cp -r -- "$dir" "$dest" # copy!
done
sybase 1215 30224 0 20:44 pts/3 00:00:00 grep dataserver
sybase 6138 6137 0 Feb04 ? 00:28:10 /u01/sybase/ASE15_0/ASE-15_0/bin/dataserver -d/u01/sybase/ASE15_0/data/aashish1_master.dat -e/u01/sybase/ASE15_0/ASE-15_0/install/aashish1.log -c/u01/sybase/ASE15_0/ASE-15_0/aashish1.cfg -M/u01/sybase/ASE15_0/ASE-15_0 -s**aashish1**
sybase 7671 1 0 Jan27 ? 00:55:50 /u01/sybase/ASE15_0/ASE-15_0/bin/dataserver -s**chaitu** -d/u01/sybase/ASE15_0/data/chaitu_master.dat -e/u01/sybase/ASE15_0/ASE-15_0/install/chaitu.log -c/u01/sybase/ASE15_0/ASE-15_0/chaitu.cfg -M/u01/sybase/ASE15_0/ASE-15_0
sybase 29479 29478 0 17:28 ? 00:00:33 /u01/sybase/ASE15_0/ASE-15_0/bin/dataserver -d/u01/sybase/ASE15_0/data/asdfg_master.dat -e/u01/sybase/ASE15_0/ASE-15_0/install/asdfg.log -c/u01/sybase/ASE15_0/ASE-15_0/asdfg.cfg -M/u01/sybase/ASE15_0/ASE-15_0 -s**asdfg** -psa
sybase 29617 29616 0 17:48 ? 00:00:33 /u01/sybase/ASE15_0/ASE-15_0/bin/dataserver -d/u01/sybase/ASE15_0/data/parbat.dat -e/u01/sybase/ASE15_0/ASE-15_0/install/parbat.log -c/u01/sybase/ASE15_0/ASE-15_0/parbat.cfg -M/u01/sybase/ASE15_0/ASE-15_0 -s**parbat**
sybase 29789 29788 0 17:57 ? 00:00:28 /u01/sybase/ASE15_0/ASE-15_0/bin/dataserver -d/u01/sybase/ASE15_0/data/ab123_master.dat -e/u01/sybase/ASE15_0/ASE-15_0/install/ab123.log -c/u01/sybase/ASE15_0/ASE-15_0/ab123.cfg -M/u01/sybase/ASE15_0/ASE-15_0 -s**ab123** -psa
[sybase#linuxerp scripts]$
I want to get the dataserver name from OS level itself without connecting to the Database.
ps -ef | grep dataserver
will get the server running or not
I tried to keep the output in a file and used grep -v on the file
Since the server name was not in exactly position, it is difficult to get the servername .
There are a couple of ways you can grab that information. One would be to pipe the grep output and use a regular expression:
ps -ef | grep dataserver | grep -oh '\-s[[:alnum:]]*' which should output something like this:
-saashish1
-schaitu
-sasdfg
-sparbat
-sab123
Another would be to use the showservers utility that comes installed with ASE, which outputs very similar to ps -ef but with CPU & Memory information as well as including other database servers such as the backup server, xp server, etc.
%> showserver
USER PID %CPU %MEM SZ RSS TT STAT START TIME COMMAND
user114276 0.0 1.7 712 1000 ? S Apr 5514:05 dataserver -d greensrv.dat -sgreensrv -einstall/greensrv+_errorlog
sybase 1071 0.0 1.4 408 820 ? S Mar 28895:38 /usr/local/sybase/bin/dataserver -d/dev/rsd1f -e/install/errorlog
user128493 0.0 0.0 3692 0 ? IW Apr 1 0:10 backupserver -SSYB_BACKUP -e/install/backup.log -Iinterfaces -Mbin/sybmultbuf -Lus_english -Jiso_1
And then pipe that into the same grep to get the information you are trying to find.
If you want to cut the -s off the front, to just get the servername itself, then you can pipe that into tr or cut.
Using tr you can tell it to delete -s from each line:
| tr -d '\-s'
Using cut you can tell it to print everything from the 3rd character to the end of the word:
| cut -c3-
Both of these will output your server names like this:
aashish1
chaitu
asdfg
parbat
ab123
Check this Question for information on using grep to grab single words.
sometimes I receive/downlad .pdf files without the "other" group. I mean -rw-r----- 1 root root 12889 Mar 26 16:41 Document.pdf.
I need to work them via php scripts (read, move,...) but I cant do it because the owner is root:root while php script is www-data:www-data.
I googled the whole internet but was unable to find a solution.
I tried with php internal commands and/or exec(), shell_exec(), system() but the result was a big hole in the water.
This is a piece of code I use. Inverting the order of the operations has no effect
$new_name = str_replace(' ','',$fname);
$new_name = preg_replace('/\(|\)/','',$new_name);
$command = 'sudo cp $full_fname $dir.$new_name';
$copy = shell_exec($command); //, $output, $retval);
$ch_perms = 'sudo chmod a+rw '. $dir.$new_name;
$permiss = exec($ch_perms, $output, $retval);
Even chown has no effect.
Any help will be really appreciated
tnx
When I do ls -l I get
-rw-r--r-- 1 jboss admin **26644936** Sep 1 21:23 MyBig.war
How do I print it as below
-rw-r--r-- 1 jboss admin **26,644,936** Sep 1 21:23 MyBig.war
The proper way to format ls output is to specify BLOCK_SIZE.
Saying:
BLOCK_SIZE="'1" ls -l
would achieve your desired result.
Quoting from the above link:
Some GNU programs (at least df, du, and ls) display sizes in “blocks”.
You can adjust the block size and method of display to make sizes
easier to read.
A block size specification preceded by ‘'’ causes output sizes to be
displayed with thousands separators.
Using sed:
$ ls_output='-rw-r--r-- 1 jboss admin 26644936 Sep 1 21:23 MyBig.war'
$ echo $ls_output | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
-rw-r--r-- 1 jboss admin 26,644,936 Sep 1 21:23 MyBig.war
Above sed command repeatedly replace the last 4 digits #### with #,###.
-e :a: Make a label named a for t command.
ta: Jump to a if substitution was successful.