2

I have a bash shell script /var/tmp/test.sh that runs on iMac in an infinite while loop.

I wish this script to run 24X7 and even when the iMac is rebooted the script should autorun.

I want to run the shell script to run 24X7 as a background process.

Can you please suggest a quick easy solution how can I?

1 Answer 1

1

Apple hasn't killed cron (yet), and so I think you have at least two options: cron and LaunchControl.

Briefly:

  • cron is "old-school" command-line Unix. It uses a crontab to define the schedule. It is free, open-source and widely used. It is easy to use - if you understand its limitations.

  • Apple's native launchd and launchctl have more capability than cron, but from your question, it appears these added capabilities are not necessary. But if you're still interested, there are some things you should know. The first thing you should know is LaunchControl; you used the word easy in your question, and this all but eliminates launchctl. LaunchControl is a 3rd-party commercial (not free) app that provides a sane GUI scheduling mechanism for launchd; it is what launchctl should have been.

The cron solution:

The first step will depend on whether or not root (Administrator) privileges are required. Since you didn't mention that they were, we'll proceed under the assumption that user privileges are all that is required by your sh script:

  1. From the Terminal app on your iMac:
% crontab -e

You may receive a prompt asking you to select a default editor for your crontab. If you're not sure which to select, choose nano as your editor.

Your crontab will open in your chosen editor.

Add the following line to the bottom of your crontab file:

@reboot /bin/sleep 10; /var/tmp/test.sh >> /Users/<YOURUSERID>/test_sh_log.txt 2>&1 

Breaking this down:

  • @reboot: tells cron to run the following command(s) when the system boots

  • /bin/sleep 10 tells cron to sleep for 10 seconds before running the next command. This may not be necessary, but if you use network services or mount file systems, the sleep time will give the system time to get those running before your script calls on them.

  • /var/tmp/test.sh is your script, w/ execute permissions properly set.

  • >> /Users/<YOURUSERID>/test_sh_log.txt 2>&1 redirects any output to a convenient log file; 2>&1 gets both stdout & stderr (error messages). This is often helpful for troubleshooting, or if you wish your shell script to output anything while running.

Once you reboot your iMac, test.sh should begin running.

Finally:

Apple's "security measures" seem to be in constant flux, and there may be additional steps required to make Apple happy. The need for these steps depends upon too many factors to cover here. If you encounter difficulties, post comments here & we'll respond.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.