ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I've got a couple of questions that I need some advice on.
1. I need to have a piece of software running in the background as root, to make some system changes based on what it finds in a file. I was either going to run a script on startup and then sleep the script for a minute, or going to set the script to run from cron. Does anyone know of any performance issues to doing it either way?
2. I need to do some basic RS232 through the serial port. It will literally be one byte out and then reading a reply of one byte in. Can bash do serial comms, if so does anyone have some source code or a website with details?
I'm trying to stay away from doing some C, but if I have no choice then... I'd really like to do it in bash.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
I am using both. For processes which I like to run at intervals of 2 minutes or larger I use cron.
For shorter intervals (I have some scripts running at 20 seconds intervals) I use sleep.
I have never checked it, but I don't expect a performance penalty for one or the other. When a process is put to sleep, well... it sleeps and doesn't use any resources.
Cron is being run once every interval anyway, whether or not you program is called so I don't see any performance issues either.
Sleep in your own program offers finer granularity than cron so it is a logical choice for shorter intervals.
Actually, there is an extra overhead via cron because it has to create a process + env before it can execute it. From a user's pt of view it may not be obvious timing-wise, but it's definitely there.
I agree that for restart intervals of less than say 5 mins, running your own daemon is (prob) better; longer than that and I'd use use cron.
There's no hard + fast rule though ...
I thought it was going to be the other way round. As if you have several processes sleeping then you have several processes monitoring for wakeup, whereas cron would be just one process sleeping waiting to fire off the others.
Still I completed the job with sleep and the performance seems OK.
That's because a daemon is sleeping, then doing something (internally).
cron is a sleeping (not much) daemon that then creates an external standalone process+env for each entry at each time requested.
In kernel terms, process creation is much more expensive than 'activating' an already extant process.
The more jobs cron has to do and more often, the more 'creations' take place.
As I said:
From a user's pt of view it may not be obvious timing-wise, but it's definitely there.
For any one job/program, the diff is usually un-noticeable, but but the cumulative load will show (eg top) if there are a lot of jobs.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.