Re: cron sh script fork
Todd C. Miller wrote: > On Mon, 15 Nov 2021 20:13:01 +0300, misc@abrakadabra.systems wrote: > > > [/opt/bin]$ cat check.sh > > #!/bin/sh > > > > _ret=$(ps aux | grep sleeploop.sh | grep -v grep | awk '{print $2}') > > test -z ${_ret} && /opt/bin/sleeploop.sh & > > By default, ps uses 80 columns so the information is probably being > cut off. I'm guessing your interactive terminal is wider than 80 > columns. You can add 'w' a few times to your ps options to extend > the width but you are much better off using pgrep for this. pgrep is better. But there are so many risks in this construct I would not trust it at all.
Re: cron sh script fork
On Mon, 15 Nov 2021 20:13:01 +0300, misc@abrakadabra.systems wrote: > [/opt/bin]$ cat check.sh > #!/bin/sh > > _ret=$(ps aux | grep sleeploop.sh | grep -v grep | awk '{print $2}') > test -z ${_ret} && /opt/bin/sleeploop.sh & By default, ps uses 80 columns so the information is probably being cut off. I'm guessing your interactive terminal is wider than 80 columns. You can add 'w' a few times to your ps options to extend the width but you are much better off using pgrep for this. - todd
Re: cron sh script fork
Your "ps" pipeline could identify other processes. If I was on your machine, I would start a long-running process with sleeploop.sh as an argument, your script sees it, and misbehaves. Don't do this. man 5 crontab A method to do this safer was -s command Only a single instance of command will be run concurrently. Additional instances of command will not be scheduled until the earlier one completes. misc@abrakadabra.systems wrote: > I have one script (sleeploop.sh) running in background and second (check.sh) > to test if sleeploop is running and if not then start it. > > > [/opt/bin]$ cat sleeploop.sh > #!/bin/sh > while true > do > sleep 5 > done > > [/opt/bin]$ cat check.sh > #!/bin/sh > > _ret=$(ps aux | grep sleeploop.sh | grep -v grep | awk '{print $2}') > test -z ${_ret} && /opt/bin/sleeploop.sh & > > > When i start check.sh from the shell it works fine; if there is no pid > check.sh > starts sleeploop.sh, otherwise it gets the pid and exiting. > If i put check.sh in cron it spawns another sleeploop.sh process every time > when triggered. > > > dmesg: > OpenBSD 7.0 (GENERIC.MP) #1: Fri Oct 29 12:04:07 MDT 2021 > > r...@syspatch-70-amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP > real mem = 1810530304 (1726MB) > avail mem = 1739616256 (1659MB) > random: good seed from bootblocks > mpath0 at root > scsibus0 at mpath0: 256 targets > mainbus0 at root > bios0 at mainbus0: SMBIOS rev. 2.7 @ 0xeb6b0 (91 entries) > bios0: vendor American Megatrends Inc. version "0608" date 08/10/2012 > bios0: ASUSTeK COMPUTER INC. P8H61-M LX3 R2.0 > acpi0 at bios0: ACPI 5.0 > acpi0: sleep states S0 S3 S4 S5 > acpi0: tables DSDT FACP APIC FPDT MCFG HPET SSDT SSDT SSDT > acpi0: wakeup devices P0P1(S4) RP01(S4) PXSX(S4) RP02(S4) PXSX(S4) RP03(S4) > PXSX(S4) RP04(S4) PXSX(S4) RP05(S4) PXSX(S4) RP06(S4) PXSX(S4) PEG0(S4) > PEGP(S4) PEG1(S4) [...] > acpitimer0 at acpi0: 3579545 Hz, 24 bits > acpimadt0 at acpi0 addr 0xfee0: PC-AT compat > cpu0 at mainbus0: apid 0 (boot processor) > cpu0: Intel(R) Pentium(R) CPU G2020 @ 2.90GHz, 2900.44 MHz, 06-3a-09 > cpu0: > FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,POPCNT,DEADLINE,XSAVE,NXE,RDTSCP,LONG,LAHF,PERF,ITSC,FSGSBASE,SMEP,ERMS,MD_CLEAR,IBRS,IBPB,STIBP,L1DF,SSBD,SENSOR,ARAT,XSAVEOPT,MELTDOWN > cpu0: 256KB 64b/line 8-way L2 cache > cpu0: smt 0, core 0, package 0 > mtrr: Pentium Pro MTRR support, 10 var ranges, 88 fixed ranges > cpu0: apic clock running at 100MHz > cpu0: mwait min=64, max=64, C-substates=0.2.1.1, IBE > cpu1 at mainbus0: apid 2 (application processor) > cpu1: Intel(R) Pentium(R) CPU G2020 @ 2.90GHz, 2900.04 MHz, 06-3a-09 > cpu1: > FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,POPCNT,DEADLINE,XSAVE,NXE,RDTSCP,LONG,LAHF,PERF,ITSC,FSGSBASE,SMEP,ERMS,MD_CLEAR,IBRS,IBPB,STIBP,L1DF,SSBD,SENSOR,ARAT,XSAVEOPT,MELTDOWN > cpu1: 256KB 64b/line 8-way L2 cache > cpu1: smt 0, core 1, package 0 > ioapic0 at mainbus0: apid 2 pa 0xfec0, version 20, 24 pins > acpimcfg0 at acpi0 > acpimcfg0: addr 0xf800, bus 0-63 > acpihpet0 at acpi0: 14318179 Hz > acpiprt0 at acpi0: bus 0 (PCI0) > acpiprt1 at acpi0: bus -1 (P0P1) > acpiprt2 at acpi0: bus 2 (RP01) > acpiprt3 at acpi0: bus -1 (RP02) > acpiprt4 at acpi0: bus -1 (RP03) > acpiprt5 at acpi0: bus -1 (RP04) > acpiprt6 at acpi0: bus -1 (RP05) > acpiprt7 at acpi0: bus 3 (RP06) > acpiprt8 at acpi0: bus 1 (PEG0) > acpiprt9 at acpi0: bus -1 (PEG1) > acpiprt10 at acpi0: bus -1 (PEG2) > acpiprt11 at acpi0: bus -1 (PEG3) > acpiec0 at acpi0: not present > acpipci0 at acpi0 PCI0: 0x0010 0x0011 0x > acpicmos0 at acpi0 > acpibtn0 at acpi0: PWRB > "PNP0C0B" at acpi0 not configured > "PNP0C0B" at acpi0 not configured > "PNP0C0B" at acpi0 not configured > "PNP0C0B" at acpi0 not configured > "PNP0C0B" at acpi0 not configured > "PNP0C14" at acpi0 not configured > acpicpu0 at acpi0: C3(350@80 mwait.1@0x20), C2(500@59 mwait.1@0x10), > C1(1000@1 mwait.1), PSS > acpicpu1 at acpi0: C3(350@80 mwait.1@0x20), C2(500@59 mwait.1@0x10), > C1(1000@1 mwait.1), PSS > acpipwrres0 at acpi0: FN00, resource for FAN0 > acpipwrres1 at acpi0: FN01, resource for FAN1 > acpipwrres2 at acpi0: FN02, resource for FAN2 > acpipwrres3 at acpi0: FN03, resource for FAN3 > acpipwrres4 at acpi0: FN04, resource for FAN4 > acpitz0 at acpi0: critical temperature is 106 degC > acpitz1 at acpi0: critical temperature is 106 degC > acpivideo0 at acpi0: GFX0 > acpivout0 at acpivideo0: DD02 > cpu0: using VERW MDS workaround (except on vmm entry) > cpu0: Enhanced SpeedStep 2900 MHz:
cron sh script fork
I have one script (sleeploop.sh) running in background and second (check.sh) to test if sleeploop is running and if not then start it. [/opt/bin]$ cat sleeploop.sh #!/bin/sh while true do sleep 5 done [/opt/bin]$ cat check.sh #!/bin/sh _ret=$(ps aux | grep sleeploop.sh | grep -v grep | awk '{print $2}') test -z ${_ret} && /opt/bin/sleeploop.sh & When i start check.sh from the shell it works fine; if there is no pid check.sh starts sleeploop.sh, otherwise it gets the pid and exiting. If i put check.sh in cron it spawns another sleeploop.sh process every time when triggered. dmesg: OpenBSD 7.0 (GENERIC.MP) #1: Fri Oct 29 12:04:07 MDT 2021 r...@syspatch-70-amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP real mem = 1810530304 (1726MB) avail mem = 1739616256 (1659MB) random: good seed from bootblocks mpath0 at root scsibus0 at mpath0: 256 targets mainbus0 at root bios0 at mainbus0: SMBIOS rev. 2.7 @ 0xeb6b0 (91 entries) bios0: vendor American Megatrends Inc. version "0608" date 08/10/2012 bios0: ASUSTeK COMPUTER INC. P8H61-M LX3 R2.0 acpi0 at bios0: ACPI 5.0 acpi0: sleep states S0 S3 S4 S5 acpi0: tables DSDT FACP APIC FPDT MCFG HPET SSDT SSDT SSDT acpi0: wakeup devices P0P1(S4) RP01(S4) PXSX(S4) RP02(S4) PXSX(S4) RP03(S4) PXSX(S4) RP04(S4) PXSX(S4) RP05(S4) PXSX(S4) RP06(S4) PXSX(S4) PEG0(S4) PEGP(S4) PEG1(S4) [...] acpitimer0 at acpi0: 3579545 Hz, 24 bits acpimadt0 at acpi0 addr 0xfee0: PC-AT compat cpu0 at mainbus0: apid 0 (boot processor) cpu0: Intel(R) Pentium(R) CPU G2020 @ 2.90GHz, 2900.44 MHz, 06-3a-09 cpu0: FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,POPCNT,DEADLINE,XSAVE,NXE,RDTSCP,LONG,LAHF,PERF,ITSC,FSGSBASE,SMEP,ERMS,MD_CLEAR,IBRS,IBPB,STIBP,L1DF,SSBD,SENSOR,ARAT,XSAVEOPT,MELTDOWN cpu0: 256KB 64b/line 8-way L2 cache cpu0: smt 0, core 0, package 0 mtrr: Pentium Pro MTRR support, 10 var ranges, 88 fixed ranges cpu0: apic clock running at 100MHz cpu0: mwait min=64, max=64, C-substates=0.2.1.1, IBE cpu1 at mainbus0: apid 2 (application processor) cpu1: Intel(R) Pentium(R) CPU G2020 @ 2.90GHz, 2900.04 MHz, 06-3a-09 cpu1: FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,DS-CPL,VMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,POPCNT,DEADLINE,XSAVE,NXE,RDTSCP,LONG,LAHF,PERF,ITSC,FSGSBASE,SMEP,ERMS,MD_CLEAR,IBRS,IBPB,STIBP,L1DF,SSBD,SENSOR,ARAT,XSAVEOPT,MELTDOWN cpu1: 256KB 64b/line 8-way L2 cache cpu1: smt 0, core 1, package 0 ioapic0 at mainbus0: apid 2 pa 0xfec0, version 20, 24 pins acpimcfg0 at acpi0 acpimcfg0: addr 0xf800, bus 0-63 acpihpet0 at acpi0: 14318179 Hz acpiprt0 at acpi0: bus 0 (PCI0) acpiprt1 at acpi0: bus -1 (P0P1) acpiprt2 at acpi0: bus 2 (RP01) acpiprt3 at acpi0: bus -1 (RP02) acpiprt4 at acpi0: bus -1 (RP03) acpiprt5 at acpi0: bus -1 (RP04) acpiprt6 at acpi0: bus -1 (RP05) acpiprt7 at acpi0: bus 3 (RP06) acpiprt8 at acpi0: bus 1 (PEG0) acpiprt9 at acpi0: bus -1 (PEG1) acpiprt10 at acpi0: bus -1 (PEG2) acpiprt11 at acpi0: bus -1 (PEG3) acpiec0 at acpi0: not present acpipci0 at acpi0 PCI0: 0x0010 0x0011 0x acpicmos0 at acpi0 acpibtn0 at acpi0: PWRB "PNP0C0B" at acpi0 not configured "PNP0C0B" at acpi0 not configured "PNP0C0B" at acpi0 not configured "PNP0C0B" at acpi0 not configured "PNP0C0B" at acpi0 not configured "PNP0C14" at acpi0 not configured acpicpu0 at acpi0: C3(350@80 mwait.1@0x20), C2(500@59 mwait.1@0x10), C1(1000@1 mwait.1), PSS acpicpu1 at acpi0: C3(350@80 mwait.1@0x20), C2(500@59 mwait.1@0x10), C1(1000@1 mwait.1), PSS acpipwrres0 at acpi0: FN00, resource for FAN0 acpipwrres1 at acpi0: FN01, resource for FAN1 acpipwrres2 at acpi0: FN02, resource for FAN2 acpipwrres3 at acpi0: FN03, resource for FAN3 acpipwrres4 at acpi0: FN04, resource for FAN4 acpitz0 at acpi0: critical temperature is 106 degC acpitz1 at acpi0: critical temperature is 106 degC acpivideo0 at acpi0: GFX0 acpivout0 at acpivideo0: DD02 cpu0: using VERW MDS workaround (except on vmm entry) cpu0: Enhanced SpeedStep 2900 MHz: speeds: 2900, 2800, 2700, 2600, 2500, 2400, 2300, 2200, 2100, 2000, 1900, 1800, 1700, 1600 MHz pci0 at mainbus0 bus 0 pchb0 at pci0 dev 0 function 0 "Intel Core 3G Host" rev 0x09 ppb0 at pci0 dev 1 function 0 "Intel Core 3G PCIE" rev 0x09: msi pci1 at ppb0 bus 1 inteldrm0 at pci0 dev 2 function 0 "Intel HD Graphics 2500" rev 0x09 drm0 at inteldrm0 inteldrm0: msi, IVYBRIDGE, gen 7 "Intel 6 Series MEI" rev 0x04 at pci0 dev 22 function 0 not configured ehci0 at pci0 dev 26 function 0 "Intel 6 Series USB" rev 0x05: apic 2 int 23 usb0 at ehci0: USB revision 2.0 uhub0 at usb0 configuration 1 interface 0 "Intel EHCI root hub" rev 2.00/1.00 addr 1 ppb1 at pci0 dev 28 function 0 "Intel 6