shell 脚本中 ruby 的 cron 作业问题

Problem with a cron job to ruby in a shell script

我有两个脚本位于 /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

试试这个:

#!/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