将进程pid剥离到log_file_name(从cron运行时出现奇怪的行为)

时间:2017-05-19 09:17:35

标签: bash awk cron sh

我很好奇是否有人在从cron运行脚本时遇到类似的问题。这行脚本应该将opentsdb_daemon.log文件复制到opentsdb_daemon_with_pid.log。目前,openTSDB仅在一个PID上运行。

!/bin/sh

cp -f /opt/opentsdb/opentsdb-2.2.0/var/log/opentsdb/opentsdb_daemon.log "/opt/opentsdb/opentsdb-2.2.0/var/log/opentsdb/opentsdb_daemon_pid_$(ps -ef | grep [o]pentsdb  | awk '{print $2}').log

运行正常。文件opentsdb_daemon_pid_76079.log已创建,但在运行fron cron时,它会创建如下内容:opentsdb_daemon_pid_63453?63454?76079.log

我试图从不同的cron用户运行它 - 具有相同的效果。我会非常感谢任何建议。

1 个答案:

答案 0 :(得分:0)

命令SELECT T.Omschrijving as Team, P.Beschrijving AS Proces, Round(Sum(P.Aantal), 2) AS Prognose, P.Maand AS PrognoseMaand, P.jaar, A.Omschrijving AS Fonds FROM tblPrognose AS P, tblTeam AS T, tblAfdeling AS A LEFT JOIN (SELECT RL.Omschrijving, I.TEAMID, I.PROCESOMS, SUM(NORMUREN_I) AS Instroom, I.Maand, I.Jaar FROM tblInstroom AS I LEFT JOIN (SELECT T.Omschrijving, R.GPSTeam, R.Procesoms FROM tblRollen AS R, tblTeam AS T WHERE T.TeamID = R.TeamID) AS RL ON (I.TEAMID = RL.GPSTEAM AND I.PROCESOMS = RL.PROCESOMS) GROUP BY I.TEAMID, I.Procesoms, I.maand, I.jaar, RL.Omschrijving) AS tblR ON (tblR.Omschrijving = T.Omschrijving) AND (tblR.Procesoms = P.Beschrijving) AND (tblR.Maand = P.Maand) AND (tblR.jaar = P.jaar) WHERE P.KPI = 'Instroom' AND P.Maand BETWEEN Month(P.Datum_vanaf) AND Month(P.Datum_tot) AND P.jaar BETWEEN year(P.Datum_vanaf) AND year(P.Datum_tot) AND P.TeamID = T.TeamID AND A.AfdelingID = T.AfdelingID AND NOT Nz(P.Beschrijving, '') = '' GROUP BY T.Omschrijving, P.Maand, P.jaar, P.Beschrijving, A.Omschrijving 应该在cron运行时返回多个PID,你得到的是由“?”分隔的所有PID

“?”是因为ps -ef | grep [o]pentsdb | awk '{print $2}'未正确显示在文件名

我认为这是因为当cron执行命令时,命令出现在进程列表中,所以:

\n也是grep [o]pentsdb;)

的grep

您可以通过两个连续的PID 63453和63454确定它,它们是“cron执行命令xxx”的进程行,而这个进程的子进程是“命令xxx”

也许解决方案可能是添加类似:

grep [o]pentsdb