我正在寻找一种方法来记录和图形显示linux进程的CPU和RAM使用情况.因为我找不到一个简单的工具(我试过zabbix和munin但是安装失败了)我开始写一个shell脚本来这样做
脚本文件通过awk解析top命令的输出并登录到csv文件.它
以下是脚本的外观
#!/bin/sh
#A script to log the cpu and memory usage of linux processes namely - redis, logstash, elasticsearch and kibana
REDIS_PID=$(ps -ef | grep redis | grep -v grep | awk '{print $2}')
LOGSTASH_PID=$(ps -ef | grep logstash | grep -v grep | awk '{print $2}')
ELASTICSEARCH_PID=$(ps -ef | grep elasticsearch | grep -v grep | awk '{print $2}')
KIBANA_PID=$(ps -ef | grep kibana | grep -v grep | awk '{print $2}')
LOG_FILE=/var/log/user/usage.log
echo $LOG_FILE …
Run Code Online (Sandbox Code Playgroud) 我有以下三个python脚本:
parent1.py
import subprocess, os, sys
relpath = os.path.dirname(sys.argv[0])
path = os.path.abspath(relpath)
child = subprocess.Popen([os.path.join(path, 'child.lisp')], stdout = subprocess.PIPE)
sys.stdin = child.stdout
inp = sys.stdin.read()
print(inp.decode())
Run Code Online (Sandbox Code Playgroud)
parent2.py:
import sys
inp = sys.stdin
print(inp)
Run Code Online (Sandbox Code Playgroud)
child.py:
print("This text was created in child.py")
Run Code Online (Sandbox Code Playgroud)
如果我用parent1.py调用:
python3 parent1.py
Run Code Online (Sandbox Code Playgroud)
它给了我预期的以下输出:
This text was created with child.py
Run Code Online (Sandbox Code Playgroud)
如果我用parent2.py调用:
python3 child.py | python3 parent2.py
Run Code Online (Sandbox Code Playgroud)
我得到相同的输出.但是在第一个例子中,我得到child.py的输出作为字节,而在第二个例子中我直接得到它作为字符串.为什么是这样?它只是python和bash管道之间的区别还是我还能做些什么来避免这种情况?
以下脚本的输出为空白.缺少什么?我正在尝试grep一个字符串
#!/bin/ksh
file=$abc_def_APP_13.4.5.2
if grep -q abc_def_APP $file; then
echo "File Found"
else
echo "File not Found"
fi
Run Code Online (Sandbox Code Playgroud) 在我阅读bash联机帮助页时,只应在以交互方式运行shell时执行.bashrc.该联机帮助页将交互定义为:
交互式shell是在没有非选项参数的情况下启动的,没有-c选项,其标准输入和错误都连接到终端(由isatty(3)确定),或者以-i选项开头.如果bash是交互式的,则设置PS1和$ - 包括i,允许shell脚本或启动文件测试此状态.
然而,使用ssh执行命令也会导致运行.bashrc,这与我期望的相反,因为该命令不是以交互方式运行的.所以,这种行为看起来像一个bug,但它似乎在我尝试过的Red Hat和bash的所有版本上都很普遍.有人能解释为什么这种行为是正确的吗?
还有一点:即使.bashrc运行,$-
并且$PS
设置好像shell是非交互式的(正如我所料).
$ grep USER /etc/passwd
USER:x:UID:GID:UNAME:/home/USER:/bin/bash
$ cat ~/.bashrc
echo bashrc:$-,$PS1
$ bash -c 'echo $-'
hBc
$ ssh localhost 'echo $-' </dev/null 2>/dev/null
USER@localhost's password:
bashrc:hBc,
hBc
$ ssh localhost 'ps -ef | grep $$' </dev/null 2>/dev/null
USER@localhost's password:
bashrc:hBc,
USER 28296 28295 0 10:04 ? 00:00:00 bash -c ps -ef | grep $$
USER 28297 28296 0 10:04 ? 00:00:00 ps -ef
USER 28298 28296 0 10:04 ? …
Run Code Online (Sandbox Code Playgroud) 我有一个bash script.sh.我可以像这样轻松滚动输出:
$ ./script.sh | less
Run Code Online (Sandbox Code Playgroud)
但是如何使输出显示自动滚动,而不必通过管道less
?换句话说,如何将该功能直接放入脚本本身?我只想像这样执行脚本:
$ ./script.sh
Run Code Online (Sandbox Code Playgroud)
我知道我可以编写一个不同的脚本来执行第一个脚本并自动管道输出但是我不想编写另一个脚本只是为了让第一个脚本做我想做的事情.明白我的意思了吗?
我有一个带有unicode行分隔符(十六进制代码2028)的文本文件。
我想使用bash删除它(我看到了Python的实现,但没有该语言的实现)。我可以使用什么命令来转换文本文件(output4.txt)以丢失Unicode行分隔符?
参见下面的vim:
我可以使用以下命令成功创建与Postgres数据库的连接:
my $settings = {
host => 'myhost',
db => 'mydb',
user => 'myuser',
passwd => 'mypasswd'
};
my $connection = DBI->connect(
'DBI:Pg:dbname=' . $settings->{'db'} . ';host=' . $settings->{'host'},
$settings->{'user'},
$settings->{'passwd'},
{
RaiseError => 1,
ShowErrorStatement => 0,
AutoCommit => 0
}
) or die DBI->errstr;
Run Code Online (Sandbox Code Playgroud)
但是我在Perl模块中留下了有价值的登录凭据(是的,我更改了它们).目前,我用psql
交互式方式发出查询.为了节省必须记住我的用户名/密码,我已将凭据放在具有权限600的文件(〜/ .pgpass)中.文件如下所示:
# host:port:database:user:passwd
myhost:5432:mydb:myuser:mypasswd
Run Code Online (Sandbox Code Playgroud)
如何安全地使用此文件("$ENV{HOME}/.pgpass"
)和DBI
模块来隐藏我的凭据?可以吗?什么是最佳做法?
简单的问题 - 在bash中测试是否存在yum或apt-get的规范方法是什么?我正在编写一个脚本,它将在客户端机器上运行并同步安装,然后为它创建一个cron作业......
我是macosx上的shell编程的新手,并且有一点问题.我编写了以下shell脚本:
#!/bin/sh
function createlink {
source_file=$1
target_file="~/$source_file"
if [[ -f $target_file ]]; then
rm $target_file
fi
ln $source_file $target_file
}
createlink ".netrc"
Run Code Online (Sandbox Code Playgroud)
当我执行这个脚本时,我收到消息ln:〜/ .netrc:没有这样的文件或目录,我不知道为什么会这样!你看到错误吗?谢谢!