-1

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.

Hajitsu
  • 764
  • 17
  • 49
  • How are they not working? It's hard to guess what is actually happening. – l0b0 Jun 02 '19 at 11:01
  • Also, have you instead tried a [restart loop](https://mywiki.wooledge.org/ProcessManagement#How_can_I_check_to_see_if_my_game_server_is_still_running.3F__I.27ll_put_a_script_in_crontab.2C_and_if_it.27s_not_running.2C_I.27ll_restart_it...)? It's a much better way to ensure the service stays up. – l0b0 Jun 02 '19 at 11:01
  • Possible duplicate of [Why is a valid path with a tilde not expanding in this cron job?](https://stackoverflow.com/questions/45401001/why-is-a-valid-path-with-a-tilde-not-expanding-in-this-cron-job) – l0b0 Jun 02 '19 at 11:07
  • 1
    try full path such as `/home/user1/launch.sh` remember `cron` has no environment variables nor your `$PATH` settings. – Dudi Boy Jun 02 '19 at 16:33
  • 1
    If you want help you need to specify how it's not working. You can for example look for error messages in the cron log. – l0b0 Jun 02 '19 at 23:16
  • @l0b0 I edit the message and add a log. thanks – Hajitsu Jun 03 '19 at 04:00
  • Cron use sh instead of bash. beware. – Debendra Jun 03 '19 at 04:10
  • @Debendra I use `#!/bin/sh` – Hajitsu Jun 03 '19 at 04:29
  • I guess you have problem with permission, can you start nginx without sudo? – Debendra Jun 03 '19 at 04:43
  • @Debendra If I run `sh Wlaunch.sh` and NGINX not running, the password prompt will be opening. – Hajitsu Jun 03 '19 at 04:52
  • So you have problem with permission. Give root permission to script and mark as executable. – Debendra Jun 03 '19 at 05:03
  • @Debendra How can i do this? sorry I'm a beginner at shell and Linux – Hajitsu Jun 03 '19 at 05:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/194352/discussion-between-debendra-and-hajitsu). – Debendra Jun 03 '19 at 05:12

2 Answers2

0

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
Debendra
  • 1,132
  • 11
  • 22
  • Thanks for your replay, In crontab, I write `* * * * * /home/hajitsu/launch.sh` and its worked. – Hajitsu Jun 03 '19 at 06:32
-1

~ won't be expanded the way it is in an interactive shell when in a crontab. Use /home/username instead.

l0b0
  • 55,365
  • 30
  • 138
  • 223