0

I have shell-script for restart some service in ruby file, kill process and run, and it's work. I want to run this shell-script every hour, so I put commands bash to crontab, cron running every hour but my shell-script only execute kill process, the three last command which is run 3 ruby files doesn't execute.

restart.sh

sudo pkill -9 -f '/home/myuser/.rvm/rubies/ruby-2.4.0/bin/ruby /home/myuser/subscribe.rb'
sudo pkill -9 -f '/home/myuser/.rvm/rubies/ruby-2.4.0/bin/ruby /home/myuser/retry1.rb'
sudo pkill -9 -f '/home/myuser/.rvm/rubies/ruby-2.4.0/bin/ruby /home/myuser/retry2.rb'
sleep .5
nohup /home/myuser/.rvm/rubies/ruby-2.4.0/bin/ruby /home/myuser/subscribe.rb &!
nohup /home/myuser/.rvm/rubies/ruby-2.4.0/bin/ruby /home/myuser/retry1.rb &!
nohup /home/myuser/.rvm/rubies/ruby-2.4.0/bin/ruby /home/myuser/retry2.rb &!

crontab

0 */1 * * * bash -lc "/home/myuser/restart.sh"
  • Your crontab command should just be 0 * * * * bash -lc "/home/myuser/restart.sh" for every hour starting at 0 minute. – Terrance Aug 25 '18 at 1:54
  • @Terrance The problem isn't about scheduler in crontab (every hour) . I have problem with 3 last command in my script which is run 3 ruby files not execute if i'm using cron. – itx Aug 25 '18 at 2:08
  • I don't recall bash having &! operator, it should be just &. – Sergiy Kolodyazhnyy Aug 25 '18 at 2:19
  • Honestly there is no need for the /1 crontab.guru/every-1-hour. But that is beside the point. I agree with @SergiyKolodyazhnyy here about having the ! at the end of your line. Did you try your script outside of crontab just to make sure it even works? – Terrance Aug 25 '18 at 2:21
  • 1
    I do have one question here, why are you running the command with bash -lc? You could probably get away with the first line in your script as #!/bin/bash so there would be no need to call bash outside of the script. Just trying to think of all possibilities as to why the script is not working properly. – Terrance Aug 25 '18 at 2:54
0

Finally, I found a solution.

The point of problem isn't permission but in a different shell environment than cron is executing in, especially run ruby file.

Since I'm use rvm for manage ruby versions, so I found example

#!/usr/bin/env bash

# load rvm ruby
source /usr/local/rvm/environments/ruby-2.1.1@projectX

bundle install
ruby /path/to/script.rb
rake do something

source : https://rvm.io/integration/cron#example-script

So, I have modified my shell-script looks like :

#!/usr/bin/env bash
sudo pkill -9 -f 'ruby /home/myuser/subscribe.rb'
sudo pkill -9 -f 'ruby /home/myuser/retry1.rb'
sudo pkill -9 -f 'ruby /home/myuser/retry2.rb'
sleep .5
source /home/mysuser/.rvm/environments/ruby-2.4.0
nohup ruby /home/myuser/subscribe.rb &
nohup ruby /home/myuser/retry1.rb &
nohup ruby /home/myuser/retry2.rb &

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.