signed in as root user, crontab -e has a single line entry:
*/1 * * * * /opt/launch-crashed-services.sh > /dev/null 2>
the /opt directory has the following permissions defined for that script
-rwxr-xr-x 1 root root 179 Sep 20 06:47 launch-crashed-services.sh
which should thus run every minute a check that nginx is running and restart if not, dumping the output to a log file.
service nginx status | grep 'active (running)' > /dev/null 2>&1
if [ $? != 0 ]
then
sudo service nginx restart > /var/log/nginx/relaunch.log # /dev/null
fi
The status of nginx is frequently inactive for long periods & said log file is empty.
Thus this script is either not being fired or somehow fires, but incorrectly.
Where is this set-up mistaken?
I don't know what to title this. Title suggestions welcome.
I'm using zsh as my default shell for the user I'm using. Whenever I try and run a script that uses bash it starts a new login shell of zsh, rather than running the script. If I change the #!/bin/bash line to #!/usr/bin/zsh it just works. Here's the kicker. Once that new shell is started the script starts working fine from that point on.
I'm using mate-terminal with vcxsrv as the X server. I initially start up a plain ubuntu bash terminal, which I then use to spawn mate-terminal, as I need the DISPLAY var setup and what not before starting mate-terminal. Anyhow, here's the script I'm attempting to start.
It's also important to note that if I reverse the shells by making bash default, and using zsh inside the script, the same thing does not happen.
How do I make this just run the script?
$ cat tmp.sh
#!/bin/bash -x
echo hello
$ ./tmp.sh
+ source /usr/sbin/start-systemd-namespace
++ SYSTEMD_EXE='/lib/systemd/systemd --system-unit=basic.target'
+++ ps -eo pid=,args=
+++ awk '$2" "$3=="/lib/systemd/systemd --system-unit=basic.target" {print $1}'
++ SYSTEMD_PID=562
++ '[' myuser '!=' root ']'
++ '[' -z 562 ']'
++ '[' 562 '!=' 1 ']'
++ export
++ sed -e 's/^declare -x //;/^IFS=".*[^"]$/{N;s/\n//}'
++ grep -E -v '^(BASH|BASH_ENV|DIRSTACK|EUID|GROUPS|HOME|HOSTNAME|IFS|LANG|LOGNAME|MACHTYPE|MAIL|NAME|OLDPWD|OPTERR|OSTYPE|PATH|PIPESTATUS|POSIXLY_CORRECT|PPID|PS1|PS4|SHELL|SHELLOPTS|SHLVL|SYSTEMD_PID|UID|USER|_)(=|$)'
++ export 'PRE_NAMESPACE_PATH=/usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/home/myuser/bin:/home/myuser/.rbenv/bin:/usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/home/myuser/bin:/home/myuser/.nvm/versions/node/v14.12.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files/WindowsApps/CanonicalGroupLimited.Ubuntu20.04onWindows_2004.2020.812.0_x64__79rhkp1fndgsc:/mnt/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Android:/mnt/c/Windows/System32:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/ProgramData/DockerDesktop/version-bin:/mnt/c/Users/trent/AppData/Local/Microsoft/WindowsApps:/home/myuser/go/bin:/home/myuser/.local/bin:/home/myuser/go/bin:/home/myuser/.local/bin'
++ PRE_NAMESPACE_PATH='/usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/home/myuser/bin:/home/myuser/.rbenv/bin:/usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/home/myuser/bin:/home/myuser/.nvm/versions/node/v14.12.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files/WindowsApps/CanonicalGroupLimited.Ubuntu20.04onWindows_2004.2020.812.0_x64__79rhkp1fndgsc:/mnt/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Android:/mnt/c/Windows/System32:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/ProgramData/DockerDesktop/version-bin:/mnt/c/Users/trent/AppData/Local/Microsoft/WindowsApps:/home/myuser/go/bin:/home/myuser/.local/bin:/home/myuser/go/bin:/home/myuser/.local/bin'
+++ pwd
++ export PRE_NAMESPACE_PWD=/home/myuser
++ PRE_NAMESPACE_PWD=/home/myuser
++ exec sudo /usr/sbin/enter-systemd-namespace ''
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 4.19.128-microsoft-standard x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Mon Oct 12 21:48:02 MDT 2020
System load: 0.0 Processes: 66
Usage of /: 7.2% of 250.98GB Users logged in: 0
Memory usage: 7% IPv4 address for eth0: 172.26.153.138
Swap usage: 0%
* Kubernetes 1.19 is out! Get it in one command with:
sudo snap install microk8s --channel=1.19 --classic
https://microk8s.io/ has docs and details.
21 updates can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable
Last login: Mon Oct 12 21:42:37 MDT 2020 on pts/3
* keychain 2.8.5 ~ http://www.funtoo.org
* Found existing ssh-agent: 108201
* Known ssh key: /home/myuser/.ssh/id_rsa
The key was this line in /etc/bash.bashrc
source /usr/sbin/start-systemd-namespace
Awhile back I got systemd working from some instructions elsewhere on the net. It must have added this line. It causes an entirely new shell to start when you're using another shell like zsh. After commenting it out, voila, no more problem.
I have created one test.sh shell script which I have scheduled using crontab -e to execute after every 1 minutes and redirecting output to a file.
test.sh
echo "Printing all Environment Var"
env
echo "Bye Bye"
Below is how my crontab look like
#crontab
0,1,2,3,4,5,6,7,8,9,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59 * * * * sudo su - admiir -c /u01/users/admiir/test.sh > /u01/app/iir/InformaticaIR/iirlog/crontab_launchsh.log
When I run ls -ltr the timestamp is getting updated but nothing is getting printed in the output file.
To run the cron every minute and to save to a file the current environment used this could be used:
* * * * * env > ~/cronenv
Next, you can start a shell like it will be run within cron by doing:
env - `cat ~/cronenv` /bin/sh
Here you could try something like:
su - admiir -c "/u01/users/admiir/test.sh > /u01/app/iir/InformaticaIR/iirlog/crontab_launchsh.log"
You can omit the sudo su and only use su
Once your script is working you could then update your cron with:
* * * * * su - admiir -c "/path/to/test.sh > /path/to/out.txt"
You could also run the cron as the specific user by doing:
sudo crontab -u username -e
I'm trying to run a sh script from crontab. If I run script manually it works perfect, but when I run it in crontab, I get errors.
The script:
#!/bin/bash
sudo tar -zcvf /var/www/nextcloud/data/nextcloud/files/backup.tar.gz /home/beno/stuff/
sudo -u www-data /usr/bin/php /var/www/nextcloud/occ file:scan --all >> /var/www/nextcloud/data/nextcloud/files/backup_log.txt
The script is supposed to make a tar backup of a folder and put it in nextcloud folder and run command files:scan, so nextcloud rescans filesystem and starts synchronization...as I read it here:
https://doc.owncloud.org/server/9.0/admin_manual/configuration_server/occ_command.html#file-operations-label
When crontab runs the script, backup.tar.gz is created, then I get following error:
An unhandled exception has been thrown:
Doctrine\DBAL\DBALException: Failed to connect to the database: An exception occured in driver: SQLSTATE[HY000] [2002] No such file or directory in /var/www/nextcloud/lib/private/DB/Connection.php:60
I'm using ubuntu16 and nextcloud11. Please help!
Following you'r request
Instead of using you'r own crontab, you have to use specic user's crontab, by sudo your crontab command:
sudo -s
then
crontab -e
now you coul add the root's 1st line:
tar -zcvf /var/www/nextcloud/data/nextcloud/files/backup.tar.gz /home/beno/stuff/
and once finished
exit
sudo -s www-data
... and so on...
or (if you could'nt run a shell under www-data):
sudo www-data crontab <<<'01 2 * * * /usr/bin/php /var/www...'
Note: This will overwrite www-data's crontab by this only line!
To prevent this, you could:
sudo www-data crontab -l
to see what contain actual crontab, then
sudo www-data crontab -l |
sed -e '$a 01 2 * * * /usr/bin/php /var/www...'
For adding you'r line, and finally
sudo www-data crontab -l |
sed -e '$a 01 2 * * * /usr/bin/php /var/www...' |
sudo www-data crontab
For replacing actual crontab by modified one.
But it could be simplier to
sudo vi /etc/cron.d/mybackups
Then add you'r rule by following time spec by user name:
21 22 * * * root tar -zcvf /var/w...
22 23 * * * www-data /usr/bin/php /var/www...
Further doc
see man -P'less +/SYSTEM' 5 crontab
if been trouble-checking for hours and can't find out why my shell script won't execute properly when using a root crontab.
I'm on a vServer eqipped with
Ubuntu 14.04.4 LTS
3.13.0-042stab113.11.
my script is a chmod 711 file:
/usr/local/sbin/bckup_script
and looks like this:
#!/bin/bash
DATE=`date +%Y-%m-%d_%H_%M_%S`
su - -c "chgrp postgres /backup/db"
su - -c "chmod 770 /backup/db"
su - -c "chown user /backup/db"
su - postgres -c "pg_dump db_name > /backup/db/${DATE}db_name.sql && pg_dumpall > /backup/db/${DATE}_all_db.out"
su - -c "rsync -a /home/user/value /backup/"
The crontab is started using
crontab -e
as
root
user
The crontab executes as far as I can tell from syslog.
When executed as root user (no crontab), the file will do what it's told to. Also my PATH is set properly and working.
I have no idea what am doing wrong.
Solution:
Thx to Jay jargot I found out what was wrong. To complete the question, here are the outputs you "asked" for:
crontab -l
#m h dom mon dow command
* * * * * bckup_script
Output of crontab was
/bin/sh: bckup_script: command not found
which lead me to the conclusion to use the absolute Path to the file which solved the problem.
my crontab -l now looks like follows and everything works like a charm!
# m h dom mon dow command
49 20 * * 1-5 /usr/local/sbin/bckup_script
Thx very much!