crontab not working in AWS EC2 instance - amazon-ec2

I am struggling to get crontabs in my AWS EC2 instance (Amazon Linux AMI) to run an R script of mine, so I wrote a very basic cronjob simply to test if crontabs are working in my instance at all:
crontab -e
* * * * * echo "Is this working"
* * * * * echo "write to script" >> testfile.txt
save the crontab
however neither of these did anything (no output in terminal from the job running / no new textfile written in my home directory)...
how to I get crontabs to work in my AWS EC2 instance?
EDIT: I tried to run the following in the command line, which didnt work...
sudo service cron start
cron: unrecognized service
EDIT2: I also get the following
ps aux | grep crond
root 2694 0.0 0.0 121604 2580 ? Ss Jan10 0:00 crond
root 5491 0.0 0.0 110472 2044 pts/0 S+ 00:52 0:00 grep --color=auto crond

You can set environment variables for cron.
Do you try to check following environment variables ?
HOME
It indicates cron user's home directory. I guess it's defined as "/"
PATH
It defines cron user's $PATH. Please this variable also.
You can see example crontab from /etc/crontab
$ cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

Related

How do i run crontab in shell script when i want to run 2 cron jobs

I have a shell script in that i am running daily cronetab
Now i wanted to run start script when system is rebooted.
mkdir /etc/cron.daily
mkdir /etc/cron.daily/filesCleanup
sudo sed -i '/filesCleanup/d' /etc/crontab | rev | cut -d":" -f2- | rev
echo "0 0 * * * root run-parts /etc/cron.daily/filesCleanup/" >> /etc/crontab
echo " #reboot /usr/bin/sleep 60;/opt/start.sh ">>/etc/cron.onReboot

crontab taks not running

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?

Start nginx from shell script on crontab

I want to check NGINX is running or not every 1 minute.
My shell script is:
#!/bin/sh
ps auxw | grep nginx | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "NGINX is not running"
/etc/init.d/nginx start
else
echo "NGINX is running"
fi
Script run with sh launch.sh correctly (If NGINX is not running, run NGINX).
The problem is when I want to run the script every 1 minute by crontab, nothing happens. Crontab list is here:
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * ~/sh launch.sh
I test * * * * * sh launch.sh, * * * * * launch.sh and * * * * * ./launch.sh but none of them work correctly.
My OS is UBUNTU 18.04.
This is log:
Jun 3 08:28:01 hajitsu-VirtualBox CRON[3239]: (root) CMD (~/launch.sh)
Jun 3 08:28:01 hajitsu-VirtualBox CRON[3240]: (hajitsu) CMD (/home/hajitsu/launch.sh)
Jun 3 08:28:01 hajitsu-VirtualBox CRON[3238]: (CRON) info (No MTA installed, discarding output)
Jun 3 08:28:01 hajitsu-VirtualBox CRON[3237]: (CRON) info (No MTA installed, discarding output)
Jun 3 08:29:01 hajitsu-VirtualBox CRON[3374]: (root) CMD (~/launch.sh)
Jun 3 08:29:01 hajitsu-VirtualBox CRON[3373]: (CRON) info (No MTA installed, discarding output)
Jun 3 08:29:01 hajitsu-VirtualBox CRON[3376]: (hajitsu) CMD (/home/hajitsu/launch.sh)
Jun 3 08:29:01 hajitsu-VirtualBox CRON[3372]: (CRON) info (No MTA installed, discarding output)
I think the command fired but nothing happend.
NGINX needs sudo privilege.
If you have sudo privileges you can modify /etc/sudoers.d/username file and execute sudo commands without password.
The file usually contains a user and a list of commands that the user can run without having to specify a password. In your case, you can run:
sudo /etc/init.d/nginx start
Add or modify your sudoers file. (replace username with your username.)
$ EDITOR=nano sudo visudo -f /etc/sudoers.d/username # EDITOR=nano sets my editor (because I am more comfortable with nano)
Copy and paste following.
You can add more sudo commands separating by comma.
username ALL=(ALL) NOPASSWD: /etc/init.d/nginx start,/etc/init.d/nginx start
Note: Commands will only execute called with sudo.
Prepend sudo in your launch.sh:
#!/bin/sh
ps auxw | grep nginx | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "NGINX is not running"
sudo /etc/init.d/nginx start
else
echo "NGINX is running"
fi
Make file executable.
$ chmod +x launch.sh
~ won't be expanded the way it is in an interactive shell when in a crontab. Use /home/username instead.

Cronjob not redirecting output

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

nc command doesn´t work in crontab

Can you please help me with this problem ?
When I issue a /bin/echo "<150>SAMBA" | /usr/bin/nc 142.17.69.11 -u 514 -w 1
in the line command my Syslog server receives the message, but when I put the script´s path in
the crontab it doesn´t receives the message...
crontab -l
SHELL=/bin/bash
PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home:/home/bin
*/3 * * * * /home/a1 > /dev/null 2>&1
pwd
/home
more a1
#!/bin/bash
/bin/echo "<150>SAMBA" | /usr/bin/nc 142.17.69.11 -u 514 -w 1

Resources