dou31797719 2017-09-14 13:15
浏览 135

获取当前运行linux的所有cronjobs

We're having a lot of cronjobs on our server and looking for a way to manage them.

A lot of functionality is covered but one thing im still working on, is a way to list all the cronjobs that are currently running.

The following does the job kind of:

class Cronjob{

static private function stringToArray($jobs = '') {
    $array = explode("
", trim($jobs)); // trim() gets rid of the last 

    foreach ($array as $key => $item) {
        if ($item == '') {
            unset($array[$key]);
        }
    }
    return $array;
}


static public function getAllJobsRunning(){
    $output = shell_exec("ps -ef | grep apache");
    return self::stringToArray($output);
}
}

But this returns more tasks and not only the cronjobs like

apache 6018 6015 0 Sep12 ? 00:00:02 /usr/sbin/httpd -DFOREGROUND
apache 6022 6015 0 Sep12 ? 00:00:08 /usr/sbin/httpd -DFOREGROUND
apache 6023 6015 0 Sep12 ? 00:00:07 /usr/sbin/httpd -DFOREGROUND
apache 7022 6015 0 14:14 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 8544 8485 0 14:41 pts/1 00:00:00 su apache
apache 8545 8544 0 14:41 pts/1 00:00:00 bash
apache 8642 8640 0 14:43 ? 00:00:00 /bin/sh -c /usr/bin/php /path_to_file/something.php > /var/log/cronlog/dailylogs.log 2>&1
apache 8643 8642 0 14:43 ? 00:00:04 /usr/bin/php /path_to_file/something.php
apache 11132 15438 0 14:58 ? 00:00:00 sh -c ps -ef | grep apache
apache 11133 11132 0 14:58 ? 00:00:00 ps -ef
apache 11134 11132 0 14:58 ? 00:00:00 grep apache
apache 11454 6015 0 Sep12 ? 00:00:06 /usr/sbin/httpd -DFOREGROUND
apache 15438 6015 0 11:29 ? 00:00:02 /usr/sbin/httpd -DFOREGROUND
apache 17761 6015 0 11:51 ? 00:00:01 /usr/sbin/httpd -DFOREGROUND
apache 18274 6015 0 Sep13 ? 00:00:06 /usr/sbin/httpd -DFOREGROUND
root 18911 11790 0 12:04 pts/0 00:00:00 su apache
apache 18912 18911 0 12:04 pts/0 00:00:00 bash
apache 21863 6015 0 12:42 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 24419 6015 0 Sep13 ? 00:00:02 /usr/sbin/httpd -DFOREGROUND
apache 24436 6015 0 Sep13 ? 00:00:02 /usr/sbin/httpd -DFOREGROUND

While im only interested in the:

apache 8642 8640 0 14:43 ? 00:00:00 /bin/sh -c /usr/bin/php /path_to_file/something.php > /var/log/cronlog/dailylogs.log 2>&1
apache 8643 8642 0 14:43 ? 00:00:04 /usr/bin/php /path_to_file/something.php

Now I could write a regex that filters out the lines I want but Im wondering if there isnt a easier way to do this.

  • 写回答

1条回答 默认 最新

  • dongshun1884 2017-09-14 13:40
    关注

    You just want the list of sheduled cronjobs or actually the activity?

    Use the following commands isntead of ps -ef | grep apache if you want to list all shedules.

    Get the crontab for all users:

    for user in $(cut -f1 -d: /etc/passwd); do crontab -u $user -l; done
    

    Get the crontab for a specific user

    for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done
    
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型