LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-06-2023, 03:20 AM   #1
finalturismo
Member
 
Registered: May 2019
Posts: 120

Rep: Reputation: Disabled
Humbly need help with if statements in an array


So iam having issues with the following statements and i cant see to figure out whats going on but here is the message .
vscode states the following statement, expressions don't expand in single quotes, use double quotes for that.

the problem is rip grep is having a bit of an issue processing the following statements and cant compare the variables.
cronjobs4servers=(
@reboot vmtouch -d -L /etc
'0 * * * * if [[ $(rc-service nfs-kernel-server status | rg -i -o "running") != "running" ]]; then rc-service nfs-kernel-server start; fi'
'0 * * * * if [[ $(rc-service smbd status | rg -i -o "running") != "running" ]]; then rc-service smbd start; fi'
'0 * * * * if [[ $(rc-service autofs status | rg -i -o "running") != "running" ]]; then rc-service autofs start; fi'
'0 * * * * if [[ $(rc-service nginx status | rg -i -o "running") != "running" ]]; then rc-service nginx start; fi'
'0 * * * * if [[ $(rc-service mariadb status | rg -i -o "running") != "running" ]]; then rc-service mariadb start; fi'
'0 * * * * if [[ $(rc-service rpcbind status | rg -i -o "running") != "running" ]]; then rc-service rpcbind start; fi'
'0 * * * * if [[ $(rc-service docker status | rg -i -o "running") != "running" ]]; then rc-service docker start; fi'
'0 * * * * if [[ $(rc-service networking status | rg -i -o "running") != "running" ]]; then rc-service networking start; fi'
'0 * * * * if [[ $(rc-service php-fpm status | rg -i -o "running") != "running" ]]; then rc-service php-fpm start; fi'
'0 * * * * if [[ $(rc-service php7.4-fpm status | rg -i -o "running") != "running" ]]; then rc-service php7.4-fpm start; fi'
'0 * * * * if [[ $(rc-service php8.0-fpm status | rg -i -o "running") != "running" ]]; then rc-service php8.0-fpm start; fi'
'0 * * * * if [[ $(rc-service php8.1-fpm status | rg -i -o "running") != "running" ]]; then rc-service php8.1-fpm start; fi'
'0 * * * * if [[ $(rc-service php8.2-fpm status | rg -i -o "running") != "running" ]]; then rc-service php8.2-fpm start; fi'
'0 * * * * if [[ $(rc-service nginx status | rg -i -o "running") != "running" ]]; then rc-service nginx start; fi'
'0 * * * * if [[ $(rc-service mariadb status | rg -i -o "running") != "running" ]]; then rc-service mariadb start; fi'
)


if [[ ${systemtype[n]} = server ]]; then
for ((cjs = 0; cjs < ${#cronjobs4servers[@]}; cjs++)); do
if [[ $(ssh -M root@fixapc.net -p "${srvmonsshport[$n]}" cat /var/spool/cron/crontabs/root | rg -o -e "${cronjobs4servers[cjs]}") != "${cronjobs4servers[cjs]}" ]]; then
echo -e "${cronjobs4servers[cjs]}" >>/var/spool/cron/crontabs/root
fi
done
fi
 
Old 02-06-2023, 04:38 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
I don't really understand why do you use rg, but would be better to use it with full path.
 
Old 02-06-2023, 06:05 AM   #3
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
Also you might be better off doing a script with a variable. then you only need to add the service type

Code:
#!/bin/sh

[ $(rc-service "$1" status | rg -i -o "running") == "running" ] || rc-service "$1" start
Then just call it like so from cron

0 * * * * $script mariadb
0 * * * * $script smbd

and so forth. Single changes to make instead of each line if it ever needed to be mucked about with.
 
Old 02-06-2023, 10:12 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,720

Rep: Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913
Code:
0 * * * * if [[ $(rc-service smbd status | rg -i -o "running") != "running" ]]; then rc-service smbd start; fi
I don't know what cron version you are running but typically have a fixed environment that uses /bin/sh. It isn't a rg problem but a "cron" problem since [[ ]] isn't defined in /bin/sh.

Last edited by michaelk; 02-06-2023 at 10:16 AM.
 
Old 02-06-2023, 10:55 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
Quote:
Originally Posted by michaelk View Post
Code:
0 * * * * if [[ $(rc-service smbd status | rg -i -o "running") != "running" ]]; then rc-service smbd start; fi
I don't know what cron version you are running but typically have a fixed environment that uses /bin/sh. It isn't a rg problem but a "cron" problem since [[ ]] isn't defined in /bin/sh.
yes, you are right. But actually
Code:
if rc-service smbd status | grep -q running >/dev/null; then
or something similar is enough. And probably
Code:
rc-service smbd status | grep -q running || rc-service smbd start
is ok too.
 
Old 02-06-2023, 12:04 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,720

Rep: Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913Reputation: 5913
True, In addition on my devuan openrc system rc-service status outputs "<service> not running ... failed". Not sure if it is different on other openrc distributions but that would make the results true regardless if the service was running or not. The OP might have to change the grep search criteria.

Last edited by michaelk; 02-06-2023 at 12:47 PM.
 
Old 02-08-2023, 11:22 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,799

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Quotes missing here?:
Code:
cronjobs4servers=(
'@reboot vmtouch -d -L /etc'
crontab uses /bin/sh and [[ ]] is not defined in sh. (Will work only if /bin/sh is a link to bash.) Use [ ] instead, and its arguments should be in quotes (not strictly needed here).
Code:
'0 * * * * if [ "$(rc-service mariadb status | rg -i -o "running")" != "running" ]; then rc-service mariadb start; fi'
)
Does rc-service provide an exit status? Then it would shorten to
Code:
'0 * * * * rc-service mariadb status >/dev/null 2>&1 || rc-service mariadb start'
Only the red part is running on the remote machine:
Code:
if [[ $(ssh -M root@fixapc.net -p "${srvmonsshport[$n]}" cat /var/spool/cron/crontabs/root | rg -o -e "${cronjobs4servers[cjs]}") != "${cronjobs4servers[cjs]}" ]]; then
echo -e "${cronjobs4servers[cjs]}" >>/var/spool/cron/crontabs/root
fi
If you want to run more on the remote machine then quoting becomes a bit tricky, because it is processed=dequoted locally and then remotely.
Code:
ssh -M root@fixapc.net -p "${srvmonsshport[$n]}" "/bin/bash -s '${cronjobs4servers[cjs]}'" << "_EOT"
  if ! fgrep -qxe "$1" < /var/spool/cron/crontabs/root
  then
    echo "$1" >>/var/spool/cron/crontabs/root
  fi
_EOT
I use fgrep (grep -F) for plain string match, so it will treat characters like * [ ] as literals. -x full line match, -q supress output (act on its exit status).
A quoted here document is protected on the local host. And passed via stdin through ssh to the remote /bin/bash. Because variables are not substituted, they are passed via arguments -> positional parameters

Last edited by MadeInGermany; 02-08-2023 at 11:39 AM.
 
  


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
humbly asking how to silently update all apt packages and do dpkg --configure -a after finalturismo Linux - Software 3 02-03-2023 07:35 AM
[SOLVED] New Hp i5 Laptop -cannot get Wine to recognise Radeon 520 chip. Can't game! Help begged humbly. atom6502 Linux - Desktop 8 08-19-2019 06:47 AM
Humbly asking for help juanfrancisco Linux - Newbie 3 11-07-2018 04:08 PM
Partitioning advice required/ humbly requested :-) brian.hussey Linux - Laptop and Netbook 6 08-23-2008 06:59 AM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:33 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