Crontab execute the command but the ruby script is not running - ruby

I've set a crontab for my ruby script.
crontab -l
12 10 * * * /bin/bash -c 'cd /home/user/path/ && /home/user/.rvm/rubies/ruby-1.9.2-p136/bin/ruby -rubygems script.rb'
the command /bin/bash -c 'cd /home/user/path/ && /home/user/.rvm/rubies/ruby-1.9.2-p136/bin/ruby -rubygems script.rb' runs well.
And in cron, cron is executing the command (I checked the syslog:
sudo tail /var/log/syslog
(user) CMD (/bin/bash -c 'cd /home/user/path/ && /home/user/.rvm/rubies/ruby-1.9.2-p136/bin/ruby -rubygems script.rb')
)
But the script is not executing exactly. I'm not getting the expected out put from it.
What may be the cause of the issue? It'll be grate if some one can help me.

Try:
12 10 * * * /bin/bash -c 'source /home/user/.rvm/scripts/rvm && cd /home/user/path/ && /home/user/.rvm/bin/ruby-1.9.2-p136 -rubygems script.rb'

If you are using .rvmrc file in your project to setup ruby version for you project. Then you need to tell rvm to trust rvmrc file in ~/.rvmrc
rvm_trust_rvmrcs_flag=1
this will disable prompt and your cron will not hang.

Try running env -i $SHELL --norc in terminal and then run the ruby script. The terminal will have the same environment as the cron

Related

shell script is not getting executed using crontab

A shell script is not getting executed when using the crontab #reboot option.
I confirmed it by running the below command
grep CRON /var/log/syslog
output:
Aug 4 22:05:57 ubuntu CRON[817]: (root) CMD (/home/a/Workspace/init.sh)
What I did so far:
sudo crontab -u root -e
#reboot /home/a/Workspace/init.sh
init.sh
#!/bin/sh
sudo sh -x /home/a/Workspace/start_zookeeper.sh >> /home/a/log.out &
sleep 10
sudo sh -x /home/a/Workspace/start_kafka.sh >> /home/a/log.out &
sleep 10
sudo sh -x /home/a/Workspace/x-bot/start.sh >> /home/a/log.out &
sleep 10
sudo sh -x /home/a/Workspace/y-bot/start.sh >> /home/a/log.out &
For anyone who is facing a similar issue, I have solved it by using absolute paths and #!bin/sh in all the shell scripts.

Bash Script with docker commands not running via Crontab

hey guys not sure what I am doing wrong here, but was hoping for some help.
I have a bash script with the following.
#!/bin/bash
docker exec -t wekan-db bash -c "scripts/wekandb_backup.sh"
docker exec -t wekan-db bash -c "rm -r /dump/*; cp -r /mongodb_backup/ /dump/mongodb_backup"
docker cp wekan-db:/dump /home/ikadmin/codes/backup/wekan/$(date +%Y-%m-%d)
Everything executes correctly when I run the bash script from the terminal.
However when I try to run it via crontab -e it does not work. Logs do show crontab trying to run it.
Just in case the bash script is currently set as 777 as well.
Any help would be appreciated
EDIT: crontab command
19 8 * * * /bin/bash /home/ikadmin/codes/scripts/backup-wekan-docker.sh

Bash shell script not executing command docker entrypoint

I have a shell script, it is not executing the "crontab" command when it is run from docker CMD. But works when I execute the file inside the docker container. What am I missing?
File entry.sh:
#!/bin/sh
echo "Starting cron"
/usr/sbin/cron -f -L 15
crontab /etc/cron.d/cron-python
Docker file:
FROM python:3
RUN apt-get update && apt-get -y install cron
COPY ./cron/entry.sh /entry.sh
COPY ./cron/crontab.txt /etc/cron.d/cron-python
RUN chmod +x /entry.sh
RUN chmod 0644 /etc/cron.d/cron-python
CMD ["./entry.sh"]
cron-python:
# crontab -e
*/1 * * * * python /app/myscript.py >> /var/log/script.log 2>&1
Following should work :
#!/bin/sh
# start cron
echo "Starting cron"
crontab /etc/cron.d/cron-python
/usr/sbin/cron -f -L 15
In your version, /etc/cron.d/cron-python does not get installed as you asked /usr/sbin/cron to run on foreground.
Also you need to copy /app/myscript.py.

How to check if command exists for a user?

I install Pip for a user (not system wide) and I would like to check that pip is installed for that user in my script that I run with sudo: sudo ./script.sh
I know to check for a command with command -v pip3 and that works when I enter it in the shell as the user.
But how can I check it in my script?
command -v pip3 exit code is 1 because I am root (because of sudo).
su -c "command -v pip3" "$SUDO_USER" has exit code 1.
sudo -u "$SUDO_USER" command -v pip3 says "command: command not found"
The simplest is
sudo -u "$SUDO_USER" -i command -v pip3
The -i option causes sudo to pass the supplied command line to the user's configured shell using its -c option, instead of trying to execute the command directly. That's necessary because command is a shell built-in; it doesn't exist as a stand-alone executable. (The -i options runs a "login" shell. There is also the -s option which runs a non-login shell. See below.)
If you want to specify a shell explicitly you could do so instead:
sudo -u "$SUDO_USER" /bin/sh -lc "command -v pip3"
Again, a login shell is forced, here by using the -l option.
As a safety feature, sudo normally resets the $PATH to a "safe" value before executing the shell (or the single command). That value will not have any of the modifications made in the /etc/profile and ~/.profile startup scripts, and without those modifications -- which add one or more user-specific directories to the path -- the shell will not find software such as pip3 which has been installed for individual users.
use following command by replacing $USER with the specific user name.
sudo -H -u $USER bash -c 'command -v pip3'
similarly, you can run any command as another user
syntax : sudo -H -u $USER bash -c 'INSERT_COMMAND_HERE'

Need to run chromium as normal user from root script

I have a kiosk that shuts down every day using rtcwake, and this uses root user. I've used && to execute the boot script after rtcwake completes, however it then starts the browser as root causing problems.
This is the command I use:
echo debian | sudo -S rtcwake -m mem -u -t $(date +%s -d '3 days 7:45') && sudo -u debian -i bash $HOME/kiosk/bin/startup.sh &.
The sudo command does work to some extent. It calls the debian user, and executes the correct script, however, it still screws up my chromium preferences.
Here is the startup script:
echo debian | sudo -S hwclock -w
export HOME=/home/debian
#log boot time
echo "Booting at" $(date) >> $HOME/kiosk/bin/logs/boot.log
#echo debian | sudo -S service connman restart
echo debian | sudo -S at 15:30 -f $HOME/kiosk/bin/shutdown.sh
crontab -u debian crontab.txt
bash $HOME/git.sh
#sudo -i -u debian
#start kiosk
export DISPLAY=:0
chromium-browser --kiosk --disable-gpu
http://localhost/kiosk/Client/main.html &
#update ip
bash /home/debian/git.sh &
I'm wondering what could be causing chrome to be executed as root. I have no idea what is going wrong.
If you execute a command with sudo it will not change environment variables like $HOME. Since per user settings are stored in $HOME, this affects the executed program if it needs such configuration files. Check this for example:
sudo -u debian bash -c 'echo $HOME'
It will print the home folder of the calling user, not the home folder of the user specified trough -u. The sudo command supports the -H command line option to handle this, however if it works depends on the security police in use.
As a solution you can use the su command instead of sudo in this case:
... && su debian -c chromium
Since su itself is executed by root you won't be asked for the password.
You must enter a password to log into a new user shell.
The command needs to be modified as follows:
echo debian | sudo -S rtcwake -m mem -u -t $(date +%s -d '3 days 7:45') && echo debian | sudo -S -u debian -i bash $HOME/kiosk/bin/startup.sh &
This avoids needing a password to log in as normal Debian user, and executes the script.

Resources