【问题标题】:Log Each Request And Output In EC2 Ubuntu or Linux在 EC2 Ubuntu 或 Linux 中记录每个请求和输出
【发布时间】:2021-10-18 14:33:02
【问题描述】:

我有一个 AWS EC2 实例,在这个实例中,我有一些 cron 作业。 这个 cron 作业看起来像:

0 5 * * mon curl -Ssi -X POST http://example.com 

我有一些手动输出,例如:

echo "output: hello..."

我想将这些操作存储在 EC2 ubuntu 或 linux 实例中的日志文件中。可能吗?有什么建议吗?

预期输出:

[2021-10-10 ...] - POST http://example.com
[2021-10-11 ...] - output: hello...

【问题讨论】:

    标签: amazon-web-services ubuntu amazon-ec2


    【解决方案1】:

    编写如下脚本~/bin/site-detector

    #!/bin/bash
    source ~/.bash_profile
    
    log_file=/tmp/site-detector.log
    curl -Ssi -X POST http://example.com  >> $log_file
    echo "Detected @ $(date)" >> $log_file
    echo " " >> $log_file
    

    使您的脚本可执行:

    chmod a+x ~/bin/site-detector
    

    更新您的 crontab 脚本:

    0 5 * * mon ~/bin/site-detector 
    

    【讨论】:

    • 感谢您的回答,但我的 crontab 包含几个请求。 0 5 * * mon curl -Ssi -X POST http://example.com ; 0 * */1 * mon curl -Ssi -X POST http://example1.com ; 0 5 * * mon curl -Ssi -X POST http://example2.com... 这个方法有效吗?
    • Crontab 配置是关于调度和时序的。 ~/bin/site-detector 脚本是关于功能的。将所有功能放在脚本中,将所有日程安排和时间安排在 crontab 行中。
    猜你喜欢
    • 2020-03-30
    • 2013-11-19
    • 1970-01-01
    • 2018-08-02
    • 2015-12-30
    • 1970-01-01
    • 2023-02-11
    • 2018-01-10
    • 2012-04-11
    相关资源
    最近更新 更多