shell script is not getting executed using crontab - shell

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.

Related

bash script didn't work in sequence by running in cron

I run a few scripts 1 by 1
cat 001.sh
sh /home/mysqldom/da-cron/f_mysqldom_nrd/5_change_nrd_tld.sh
sh /home/mysqldom/da-cron/f_mysqldom_nrd/5_proxy_removed.sh
sh /home/mysqldom/da-cron/f_mysqldom_nrd/6_sync_nrd.sh
The last script wont work... if I run manually it work very well...
the script is
cat 6_sync_nrd.sh
source /home/mysqldom/da-cron/var.sh
cd /home/mysqldom/da-cron/f_mysqldom_nrd/
mysql -u mysqldom_fnrd -p$mysqldom_fnrd_password -D mysqldom_fnrd -e "UPDATE \`$yesterday\` SET sync='$yesterday';"
mysql -u mysqldom_fnrd -p$mysqldom_fnrd_password -D mysqldom_fnrd -e "DELETE FROM \`$yesterday\` WHERE domain_name = 'domain_name';"
sed s/change_database/$yesterday/g update.conf > $yesterday.conf
/usr/share/logstash/bin/logstash -f $yesterday.conf --path.data /var/lib/logstash108
rm -rf nohup.out
The 6 has to be run after 5
any idea whats worn in it

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.

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.

Can't execute script from within rc.local

Within rc.local I have
sudo -H -u myUser -s -- "cd /home/myUser/parlar && /usr/local/bin/meteor &"
I want to test it but when I execute that with
myUser:~$ sudo service rc.local start
/bin/bash: cd /home/myUser/parlar && /usr/local/bin/meteor &: No such file or directory
If I execute the command
cd /home/myUser/parlar && /usr/local/bin/meteor &
it works
How can I execute rc.local so that it changes into the relevant directory, and runs the command as the requested user?
Whatever arguments you give to sudo after -- are considered as command & its arguments.
There is no command/executable named "cd /home/myUser/parlar && /usr/local/bin/meteor". You can however, start bash & run the command within that bash shell.
e.g.
sudo -H -u myUser -s -- bash -c "cd /home/myUser/parlar && /usr/local/bin/meteor &"
Since the first command is cd, this alternate approach may also work:
sudo -H -u myUser -s -i PWD=/home/myUser/parlar -- /usr/local/bin/meteor
To see the log of rc.local itself, it's better to run these commands:
**systemctl restart rc-local.service**
**systemctl status rc-local.service**
May be it can be help full for better trouble shooting

Crontab execute the command but the ruby script is not running

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

Resources