Home > database >  Problem with a cron job to ruby in a shell script
Problem with a cron job to ruby in a shell script

Time:09-23

I have two scripts located in /home/luis, user that has admin rights. One is .sh and one is some ruby code that i'm trying to execute to generate a txt file in the same folder.

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

If i run from the terminal ./md5_validation.sh everything works fine and the ruby code is executed. But when i start the crontab with sudo systemctl start cron, after 1 minute only the ./md5_validation.sh bash code is executed and the line

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

included the .sh file script is like ignored and it doesn't generate the txt file i need.

Command used to create the crontab:

sudo crontab -e

Content of the crontab

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

more useful info

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

CodePudding user response:

Try this:

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

If it doesn't work. Maybe there really has a problem.

You can check the /var/log/syslog

If you see the log contain (CRON) info (No MTA installed, discarding output) Redirect the command line output into a random file to see the log:

*/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

Then see the cron.log if any error occurs.

  • Related