LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-07-2020, 02:20 PM   #1
zackryderrrr
LQ Newbie
 
Registered: Oct 2020
Posts: 4

Rep: Reputation: Disabled
Crontab fails to run a shell


there's a command to start up a service which is
Code:
su -s /bin/bash nuance -c '$JAVA_HOME/bin/java -Dlog4j.configurationFile=$NLPS_HOME/config/log4j2.xml -jar $NLPS_HOME/lib/nlps.jar --spring.config.location=$NLPS_HOME/config/,$NUANCE_DATA_DIR/system/config/User-nlps01.properties  watcher.RestartOnFailure=FALSE watcher.SendAlarmsToWatcher=FALSE > /dev/null 2>&1 &'
I want this command to run at startup in my rhel 7 linux. So I tried crontab, which is @reboot sh /execute/nlp.sh

But it doesn't work, I don't know why. Please tell me what I'm doing wrong I'm so stuck Inside nlp.sh:
Code:
#!/bin/bash
/bin/su -s /bin/bash nuance -c '$JAVA_HOME/bin/java -Dlog4j.configurationFile=$NLPS_HOME/config/log4j2.xml -jar $NLPS_HOME/lib/nlps.jar --spring.config.location=$NLPS_HOME/config/,$NUANCE_DATA_DIR/system/config/User-nlps01.properties  watcher.RestartOnFailure=FALSE watcher.SendAlarmsToWatcher=FALSE > /dev/null 2>&1 &'

Last edited by zackryderrrr; 10-08-2020 at 02:28 AM.
 
Old 10-07-2020, 07:28 PM   #2
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,366
Blog Entries: 28

Rep: Reputation: 6162Reputation: 6162Reputation: 6162Reputation: 6162Reputation: 6162Reputation: 6162Reputation: 6162Reputation: 6162Reputation: 6162Reputation: 6162Reputation: 6162
Please surround the code with "code" tags, which become available when you click the "Go Advanced" button beneath the compose/edit post window.

It will make the code much easier to read and critique.
 
Old 10-07-2020, 07:34 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
A cron job wouldn't typically have those environment variables set. cron doesn't have the same environment as your user, unless you set it up...typically within the script itself...which means you could just code the actual paths in the command(s)

But yes, please edit your OP to put the code in code tags. Very hard to see what you're actually doing without them. There's a how-to link in my sig
 
Old 10-07-2020, 08:14 PM   #4
sgosnell
Senior Member
 
Registered: Jan 2008
Location: Baja Oklahoma
Distribution: Debian Stable and Unstable
Posts: 1,943

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
The su -s won't work because it requires a password to be entered. Delete that and run the rest from root's crontab. I have no idea what the rest does, so I can't say if it will work as entered.
 
Old 10-07-2020, 09:28 PM   #5
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 483

Rep: Reputation: 153Reputation: 153
If you are trying to start a service at boot time you should create a systemd service and add it to your default target. Using crontab for this is the wrong way. Crontab is for regularly scheduled operations such as backups. It is not for starting services.
 
1 members found this post helpful.
Old 10-08-2020, 01:33 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,004

Rep: Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338
additionally would be better to write a script for that command instead of putting it directly into crontab.
replace redirections (instead of /dev/null into a logfile) to see what's wrong.

But as it was mentioned: crontab uses a different environment, so probably your script is unable to run from cron. Also a service should be configured instead.
 
Old 10-08-2020, 01:57 AM   #7
lpwevers
Member
 
Registered: Apr 2005
Location: The Netherlands
Distribution: SuSE, CentOS
Posts: 181

Rep: Reputation: 21
As said by @tofino_server, try creating a systemd service. Something like this should work:
Code:
[Unit]
Description=<Name of your service>
After=network.target

[Service]
User=nuance
Group=users
PIDFile=/var/run/<your pid>.pid
Environment=JAVA_HOME=<path to java home>
Environment=NLPS_HOME=<path to NLPS home>
Environment=NUANCE_DATA_DIR=<path to data dir>
WorkingDirectory=${NLPS_HOME}
ExecStart= ${JAVA_HOME}/bin/java -Dlog4j.configurationFile=${NLPS_HOME}/config/log4j2.xml -jar ${NLPS_HOME}/lib/nlps.jar --spring.config.location=${NLPS_HOME}/config/,${NUANCE_DATA_DIR}/system/config/User-nlps01.properties watcher.RestartOnFailure=FALSE watcher.SendAlarmsToWatcher=FALSE > /dev/null 2>&1
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=60
KillMode=process

[Install]
WantedBy=multi-user.target
Save it in /etc/systemd/system/nlps.service. Then try starting it with something like:
Code:
systemctl start nlps
If that works, enable the service so it starts at runtime:
Code:
systemctl enable nlps
If at first it doesn't work and you need to change your service file, don't forget to run
Code:
systemctl daemon-reload
before trying to start the service again.
 
Old 10-08-2020, 01:32 PM   #8
zackryderrrr
LQ Newbie
 
Registered: Oct 2020
Posts: 4

Original Poster
Rep: Reputation: Disabled
Yes, I've edited my post with "code". First thank you guys for the responses, much appreciated as other forums didn't even respond. And yeah crontab is a bad option I guess and I even included all the environment variables like "export NLPS_HOME=/path" also I gave full paths without '$ path' and the shell script works fine. But I want this to be executed at the startup. If the shell script works why can't crontab or even a simple systemd service doesn't execute properly? Please enlighten me guys!
 
Old 10-08-2020, 01:37 PM   #9
zackryderrrr
LQ Newbie
 
Registered: Oct 2020
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by lpwevers View Post
As said by @tofino_server, try creating a systemd service. Something like this should work:
Code:
[Unit]
Description=<Name of your service>
After=network.target

[Service]
User=nuance
Group=users
PIDFile=/var/run/<your pid>.pid
Environment=JAVA_HOME=<path to java home>
Environment=NLPS_HOME=<path to NLPS home>
Environment=NUANCE_DATA_DIR=<path to data dir>
WorkingDirectory=${NLPS_HOME}
ExecStart= ${JAVA_HOME}/bin/java -Dlog4j.configurationFile=${NLPS_HOME}/config/log4j2.xml -jar ${NLPS_HOME}/lib/nlps.jar --spring.config.location=${NLPS_HOME}/config/,${NUANCE_DATA_DIR}/system/config/User-nlps01.properties watcher.RestartOnFailure=FALSE watcher.SendAlarmsToWatcher=FALSE > /dev/null 2>&1
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=60
KillMode=process

[Install]
WantedBy=multi-user.target
Save it in /etc/systemd/system/nlps.service. Then try starting it with something like:
Code:
systemctl start nlps
If that works, enable the service so it starts at runtime:
Code:
systemctl enable nlps
If at first it doesn't work and you need to change your service file, don't forget to run
Code:
systemctl daemon-reload
before trying to start the service again.
Yes I tried your systemd service but it fails. The spring breaks while starting this as a service, it really looks like a neat service but it fails.
 
Old 10-08-2020, 02:15 PM   #10
sgosnell
Senior Member
 
Registered: Jan 2008
Location: Baja Oklahoma
Distribution: Debian Stable and Unstable
Posts: 1,943

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
You can put the script in /etc/rc.local and it will run at system startup. You will need to edit it to make a proper script, but it shouldn't be that hard.
 
Old 10-09-2020, 12:36 AM   #11
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 483

Rep: Reputation: 153Reputation: 153
Quote:
You can put the script in /etc/rc.local and it will run at system startup. You will need to edit it to make a proper script, but it shouldn't be that hard.
You may not have heard but using /etc/rc.local does not work on systemd installations. The file /etc/rc.local does not even exist on modern systemd Linuxes. You need to create a systemd service to call /etc/rc.local at boot time on systemd installations as well as create the /etc/rc.local bash script file itself and make it executable with chmod.

https://www.linuxbabe.com/linux-serv...l-with-systemd

Given this fact the proper way to do what the OP needs is with a systemd service as lpwevers showed.

Since Debian is also a systemd distro your advice would not work there either.

Last edited by tofino_surfer; 10-09-2020 at 02:50 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gdrive from crontab does'nt work , shell is ok from command line but failed from crontab sazdovskij Linux - Newbie 3 05-20-2019 12:25 PM
[SOLVED] Script file fails when run from crontab. karelvdm Linux - Newbie 3 12-29-2010 03:59 AM
man crontab(5) vs crontab(1) Canis Polaris Linux - Newbie 2 06-04-2008 04:03 PM
system-wide crontab in /etc/crontab ner Linux - General 2 11-18-2003 12:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration