sh用于显示包含每个用户的crontab的脚本(代码片段)

author author     2022-12-19     476

关键词:

#!/bin/bash

# System-wide crontab file and cron job directory. Change these for your system.
CRONTAB='/etc/crontab'
CRONDIR='/etc/cron.d'

# Single tab character. Annoyingly necessary.
tab=$(echo -en "\t")

# Given a stream of crontab lines, exclude non-cron job lines, replace
# whitespace characters with a single space, and remove any spaces from the
# beginning of each line.
function clean_cron_lines() 
    while read line ; do
        echo "$line" |
            egrep --invert-match '^($|\s*#|\s*[[:alnum:]_]+=)' |
            sed --regexp-extended "s/\s+/ /g" |
            sed --regexp-extended "s/^ //"
    done;


# Given a stream of cleaned crontab lines, echo any that don't include the
# run-parts command, and for those that do, show each job file in the run-parts
# directory as if it were scheduled explicitly.
function lookup_run_parts() 
    while read line ; do
        match=$(echo "$line" | egrep -o 'run-parts (-1,2\S+ )*\S+')

        if [[ -z "$match" ]] ; then
            echo "$line"
        else
            cron_fields=$(echo "$line" | cut -f1-6 -d' ')
            cron_job_dir=$(echo  "$match" | awk 'print $NF')

            if [[ -d "$cron_job_dir" ]] ; then
                for cron_job_file in "$cron_job_dir"/* ; do  # */ <not a comment>
                    [[ -f "$cron_job_file" ]] && echo "$cron_fields $cron_job_file"
                done
            fi
        fi
    done;


# Temporary file for crontab lines.
temp=$(mktemp) || exit 1

# Add all of the jobs from the system-wide crontab file.
cat "$CRONTAB" | clean_cron_lines | lookup_run_parts >"$temp" 

# Add all of the jobs from the system-wide cron directory.
cat "$CRONDIR"/* | clean_cron_lines >>"$temp"  # */ <not a comment>

# Add each user's crontab (if it exists). Insert the user's name between the
# five time fields and the command.
while read user ; do
    crontab -l -u "$user" 2>/dev/null |
        clean_cron_lines |
        sed --regexp-extended "s/^((\S+ +)5)(.+)$/\1$user \3/" >>"$temp"
done < <(cut --fields=1 --delimiter=: /etc/passwd)

# Output the collected crontab lines. Replace the single spaces between the
# fields with tab characters, sort the lines by hour and minute, insert the
# header line, and format the results as a table.
cat "$temp" |
    sed --regexp-extended "s/^(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(.*)$/\1\t\2\t\3\t\4\t\5\t\6\t\7/" |
    sort --numeric-sort --field-separator="$tab" --key=2,1 |
    sed "1i\mi\th\td\tm\tw\tuser\tcommand" |
    column -s"$tab" -t

rm --force "$temp"

crontab不执行sh脚本手动可以执行crontab执行失败【亲测】

参考技术Acrontab不执行sh脚本,手动可以执行网上很多都说cron服务未安装,未使用绝对路径。没说到重点。不过是要确认一下以上两点。确认无误时再看下步。查看运行记录命令tailf/var/log/cron用户登陆Linux操作系统的时候,”/etc/pr... 查看详情

linux设置开机自启动脚本的最佳方式

...添加自己的脚本然后,增加脚本执行权限第二种方式是在crontab中设置也可以设置每次登录自动执行脚本,在/etc/profile.d/目录下新建sh脚本,/etc/profile会遍历/etc/profile.d/*.sh另外,几个脚本的区别:(1)/etc/profile:此文件为系统的... 查看详情

sh用于安装docker的用户数据脚本(代码片段)

查看详情

在从 crontab 运行的 sh 脚本中找不到命令 [重复]

】在从crontab运行的sh脚本中找不到命令[重复]【英文标题】:Commandnotfoundinshscriptrunningfromcrontab[duplicate]【发布时间】:2021-01-1423:57:26【问题描述】:我有一个运行的sh文件:python-mgrafana_backup.clisave--config$settings_file。我从crontab运行... 查看详情

sh用于封装发送用户通知的shell脚本(代码片段)

查看详情

sh用于创建mysql数据库和用户的shell脚本(代码片段)

查看详情

centos中crontab定时任务找不到命令问题

...定时执行指定的脚本或二进制程序文件。但是发现有时候crontab在执行的时候,总是提示找不到命令。但是手动执行的时候,却没有该问题。crontab定时任务的执行日志,可以在/var/log/目录中找到。出现上面的问题,其实就是环境... 查看详情

crontab命令行执行成功,定时任务报错

crontab的定时任务不能自动执行,但是手动执行脚本一直能成功。查到最后,发现是脚本里用了系统的环境变量。1.因为我只有普通用户(sh用户)的权限,首先自己可以写一个简单的脚本测试一下自己计划任务能否使用。(我的... 查看详情

crontab 没有运行我的脚本

】crontab没有运行我的脚本【英文标题】:crontabisnotrunningmyscript【发布时间】:2012-10-2803:46:13【问题描述】:我是cron作业的新手。我阅读了post,了解如何使用crontab编写cron作业。所以我的crontab看起来像这样:1****/Users/apple/Desktop/w... 查看详情

linux下编写定时任务crontab

参考技术Alinux下的crontab服务:1、crontab是用来让使用者在固定时间或固定间隔执行程序之用在linux平台上如果需要实现任务调度功能可以编写cron脚本来实现。以某一频率执行任务linux缺省会启动crond进程,crond进程不需要用户启动... 查看详情

linux中每个用户都可以使用crontab么?

不是的,有些普通用户是无法使用crontab的,需要注册用户后购买一定时间使用权才可以使用。参考技术A作为Linux用户,你可以控制谁有权使用crontab命令。可以使用/etc/cron.deny和/etc/cron.allow文件来控制。默认情况下,只有一个/etc/... 查看详情

sh一个简单的bash脚本,用于在nexus设备上缓存和测试ubuntutouch频道。请参阅https://developer.ubuntu.com/en/start/ubuntu-for-de(代

查看详情

sh用于解析nginx访问日志的bash脚本:https://sysadmins.co.za/bash-script-to-parse-and-analyze-nginx-access-logs/(代

查看详情

我的linux定时任务不起作用是为啥?

我在root下输入crontab-e键入*/1****/home/mission.sh那个文件内容是echo"helloworld">/dev/pts/1目的是每分钟在屏幕上打印出helloworld但是死活就是不执行cron进程始终是开着的手动执行下/home/mission.sh,看是否会在屏幕上打印出helloworld... 查看详情

我的linux定时任务不起作用是为啥?

我在root下输入crontab-e键入*/1****/home/mission.sh那个文件内容是echo"helloworld">/dev/pts/1目的是每分钟在屏幕上打印出helloworld但是死活就是不执行cron进程始终是开着的手动执行下/home/mission.sh,看是否会在屏幕上打印出helloworld... 查看详情

crontab及脚本添加crontab

  crontab-u//设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数   crontab-l  列出某个用户cron服务的详细内容  crontab-r  删除没个用户的cron服务  crontab-e  编辑某个用户的... 查看详情

sh用于克隆用户的所有授权和/或公共存储库的脚本。使用https进行克隆。访问存储库的用户可能是dif(代码片段)

查看详情

如何用shell脚本定时启动tomcat服务

写一个重启tomcat的shell脚本,然后用crontab定时执行这个脚本即可。参考技术Acrontab是linux常用的定时任务处理工具,crontab-l查看当前用户定时任务crontab-e编辑用户定时任务本回答被提问者采纳 参考技术B#/bin/bash/usr/local/tomcat/bin/star... 查看详情