【问题标题】:Problem with a cron job to ruby in a shell scriptcron 作业在 shell 脚本中的 ruby​​ 问题
【发布时间】:2021-11-16 01:12:25
【问题描述】:

我有两个脚本位于 /home/luis,具有管理员权限的用户。一个是 .sh,一个是一些 ruby​​ 代码,我正在尝试执行这些代码以在同一文件夹中生成一个 txt 文件。

linux_users.rb

class User
  attr_reader :username, :home_directory
  def initialize(username, home)
    @username = username
    @home_directory = home
  end
end

unix_users = []
File.open('/etc/passwd').readlines.each do |line|
  split = line.split(':')
  unix_users << User.new(split[0], split[5])
end

file = File.open("users.txt", "w")
unix_users.each do |user|
  file.puts "#{user.username}:#{user.home_directory}"
end
file.close

md5_validation.sh

#!/bin/bash
/home/luis/.rbenv/shims/ruby /home/luis/linux_users.rb
if [[ ! -f "/var/log/current_users.txt" ]]                             #If the file current_users doesn't exist --> Create it
then
    echo "creating current_users.txt and user_changes.txt"
    touch /var/log/current_users.txt /var/log/user_changes.txt
fi
md5_users="$(md5sum "/home/luis/users.txt" | cut -d' ' -f1)"           #Storing the md5 hash into a variable
if [ -s /var/log/current_users.txt ]                                   #If current_users.txt is not empty,
then
    current_users_txt_hash=`cat /var/log/current_users.txt`            #collect it's content.
    if [ "$md5_users" == "$current_users_txt_hash" ]                   #if the linux users hash match the old one, fine,
    then
    echo "No changes in the MD5 hash detected"
  else                                                               #otherwise store the new hash into current_users.txt
  echo "$md5_users" > "/var/log/current_users.txt"
  echo "A change occured in the MD5 hash"
  now=$(date +"%m_%d_%Y_%T")                                         #creating a user_changes.txt file that logs the changing
  echo "$now changes occured" > "/var/log/user_changes.txt"
  fi
else
    echo "Storing the MD5 hash into the filename.."                    #store the linux users hash into the current_users.txt (first script launch)
    echo "$md5_users" > "/var/log/current_users.txt"
fi

如果我从终端运行 ./md5_validation.sh 一切正常,并且执行 ruby​​ 代码。但是当我用 sudo systemctl start cron 启动 crontab 时,1 分钟后只执行 ./md5_validation.sh bash 代码和行

/home/luis/.rbenv/shims/ruby /home/luis/linux_users.rb

包含的 .sh 文件脚本就像被忽略了,它不会生成我需要的 txt 文件。

用于创建 crontab 的命令:

sudo crontab -e

crontab 的内容

*/1 * * * * /usr/bin/sh /home/luis/md5_validation.sh

更多有用的信息

➜  log whereis sh              
sh: /usr/bin/sh /usr/share/man/man1/sh.1.gz
➜  log whereis ruby
ruby: /home/luis/.rbenv/shims/ruby

【问题讨论】:

    标签: ruby linux bash shell cron


    【解决方案1】:

    试试这个:

    #!/bin/bash
    cd /home/luis
    /home/luis/.rbenv/shims/ruby /home/luis/linux_users.rb
    # ...
    

    如果它不起作用。 也许真的有问题。

    您可以查看/var/log/syslog

    如果您看到日志包含(CRON) info (No MTA installed, discarding output) 将命令行输出重定向到随机文件中查看日志:

    */1 * * * * /usr/bin/sh /home/luis/md5_validation.sh > /home/luis/cron.log
    
    # btw, you can dismiss the /usr/bin/sh if you do chmod +x md5_validation.sh
    # so it can be little shorter
    # */1 * * * * /home/luis/md5_validation.sh > /home/luis/cron.log
    

    如果发生任何错误,请查看 cron.log。

    【讨论】:

    • 添加 cd /home/luis 解决了一切 :) 谢谢
    • 顺便说一句,我认为您创建的 .txt 文件应该位于/root/users.txt
    猜你喜欢
    • 2012-10-03
    • 2020-03-06
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多