| Conditional Execution within Crontab |
| UNIX / Linux - General UNIX |
| Wednesday, 09 March 2011 09:47 |
|
Conditional execution is a term used to describe the execution of processes or commands based on specfic conditions. This can also be thought of as conditional logic. Like with most scripting or programming languages conditional logic is built upon the use of return codes. A return code (exit status) is returned upon process completion to determine success or failure. A return code of 0 is set if the process has been successfully executed, and a non-zero is set on failure. You can test this by running a command such as pgrep crondon a system that you already know is running crond. [root@server ~]# pgrep crondThe $? allows you to echo the return code. In this case crond was successful but crond123 was not as there is no process running with this name. Using this logic we can create a single crontab entry to allow us to run a script only if it is not already running. * * * * * /usr/sbin/pgrep example.sh ; [ $? != 0 ] && /path/example.sh The logic of this works by [ $? != 0 ] evaluating the return code of /usr/sbin/pgrep example.sh. If this evaluation is successful (i.e a return code of 0 is not found from the pgrep command) then a return code of 0 is produced from this test statement. The test statement return code is then passed to &&. && works by only executing any further commands if a return code of 0 is received. This then allows us to only run the /path/example.sh script based on a return code of 0 from [ $? != 0 ] (test statement). |
Latest Articles
- F5 LTM VE 10.2.x - Interfaces not recognised
- Cisco ASA - Security Levels / NAT Control
- F5 LTM - OneConnect
- Django - CSRF verification failed. Request aborted.
- F5 LTM VE - Unable to attach to PCI device 02:01.00 for Interface 1.1
- F5 LTM - Connection Management
- Brocade ADX - FTP
- PKI - Chain of Trust
- Juniper SRX - Site to Site VPN using a Dynamic IP address
- F5 LTM - How do I perform software installations ?
- Juniper SRX - NAT
- Juniper SRX - How to configure a route based VPN
- Juniper SRX - Dynamic VPN
- Juniper SRX - How to configure a policy based VPN
- Brocade ADX - NAT
- Brocade ADX - CSW nested rules
- How do I upgrade a Juniper SRX Series gateway
- Cisco ASA - How do I capture ARP`s ?
- Juniper SRX - Configuring Source NAT with pool
- Running a packet capture on a Juniper SRX
Popular
- Proxy ARP – SPLAT
- Check Point Commands
- IPSO - Commands
- ASA 8.3 - How to configure NAT
- vSphere - Creating User and Group Permissions
- PEMU - Free Cisco PIX Firewall Emulator / Simulator
- Configuring Wireless Connectivity within Backtrack 4 r2
- Juniper Netscreen Commands
- Juniper Netscreen - NAT Explained
- How do I install snmpwalk / snmpget using Yum ?
- Netscreen - NSRP
- ESX Convertor - The session is not authenticated
- Troubleshooting a Netscreen Site 2 Site VPN
- ESX - ViClient Cannot connect to host
- Endpoint Connect Installation / Troubleshooting Guide
- Check Point - How to Reset SIC
- ESXi - Connecting to a named pipe
- Netscreen - Routing Basics / Virtual Routers / PBR
- Check Point Logging Troubleshooting Guide
- Configuring Windows 2008 R2 as an NTP Server
