Thread: cron and c.out

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    44

    cron and c.out

    Hello cboard,

    My question(s) is more OS related than C because I’m trap at not knowing what CRON full capabilities are. I have 3 well crafted c-files (id1 - id3.out) that works standalone without a hitch. The only thing it does is add *1* to a big number in separate file of difference size numbers and save it for the next day. You may ask why I choose to play with big numbers?

    Well that was what everyone was trying to do because c is limited to 256. A DATABASE will end up with more lines then that SO I dived in and got something working. For simplicity:

    Code:
    id1.txt = 1000000021
    id2.txt = 1000000323
    id3.txt = 1000000011
    The expected result for a complete run within 1 minute or within 24 hours would be:

    Code:
    id1.txt = 1000000022       <<-- this one works
    id2.txt = 1000000324
    id3.txt = 1000000012
    But no!

    Only the first line works in CRON for me and it seem this is the way for many other who have the same type issue that I saw during my searches on google; 24/7 for many days. I learn a lot but could not figure a solution or the truth about cron.

    What has thrown me off is ALL that I have read there is no simple statement that directly says *there is only ONE CRON per user; per system that can be run within 24 hours*. If this IS true, I would be one-step closer to throwing in the towel. If this is not the case could someone please make all of below work thru whatever fashion possible?

    These are my cron statements. They do not including the other 20 or more ways I found while googling, and tried nearly all of them to oblivion:

    I ran this in /var/cron/tabs/root. I also ran this directly in /etc/cron-tab. This is the example list and both methods burped the same result.

    Code:
    00 0 * * *    root    /bin1/id1.out……….  Midnight every minute
    10 0 * * *    root    /bin1/id2.out……….  Midnight every 10 minute
    20 0 * * *    root    /bin1/id3.out……….  Midnight every 20 minute
    If my setting here are not correct, trust me; trying to be like Paul Simon, I tried all 50 ways but I got same result (it do-nothing for lines 2 and 3 but line one always works). You find this out by checking the cron.log. In cron.log it accually read line 2 and 3 but show nothing as output in those files.

    Each of them is suppose to increase a big numbers by one… I hope I presented enough information/ideas about my situation that can lead to a solution.

    Have a Great day!


    PS:

    This is all about:

    How to Automate INSERT_INTO using C?


    As you see, the only showstopper is that you lose the ability to include a continuous id INT field in mysql when using Automated INSERT_INTO or whatever it is call. Now here comes cron to not be who I thought he was

    I hope I have missed something because this should not had happen.


    Sorry for taking so long
    Last edited by jc2020; 03-14-2020 at 06:07 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jc2020
    Well that was what everyone was trying to do because c is limited to 256. A DATABASE will end up with more lines then that
    I don't understand what you're saying here: C is limited to 256 what? A database will end up with more lines? But a database usually conceptually consists of records, not lines, even if a record in a database might be represented by a line in a text file.

    Next, what does "Midnight every minute" mean? You either want the program to run at midnight every day (or in some other frequency of more than a day), or you want it to run every minute, but it makes no sense to say "Midnight every minute" since midnight only occurs once every day, not every minute. Likewise for "Midnight every 10 minute".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    Yep, it’s my terminology and even my English that gets in the way. Thanks for the reminder laserlight. I’ll work to do better.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    crontab(5): tables for driving cron - Linux man page
    You can see plenty of examples which show more complicated files that what you're claiming.

    Post your actual crontab file (the output of crontab -l), not the ... riddled thing you posted.

    > because c is limited to 256. A DATABASE will end up with more lines
    What do you think your database s/w is written in?

    None of what you posted makes any sense whatsoever.

    Your source code for "I have 3 well crafted c-files" wouldn't go amiss either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    None of what you posted makes any sense whatsoever?
    Why are we talking down on your fellow member?

    Allow me to pick into my own thread:

    My question(s) is more OS related than C because I’m trap at not knowing what CRON full capabilities are. I have 3 well crafted c-files (id1 - id3.out) that works standalone without a hitch.
    I’ll comment on this myself as an outsider who may understand common people. jc2020 are you saying you are the coding king or are you simply assuring us that your files are not the issue? Yes; it don’t take rocket science to see that much.

    What has thrown me off is ALL that I have read there is no simple statement that directly says *there is only ONE CRON per user; per system that can be run within 24 hours*.
    Hey jc2020, if I never read that anywhere on the WWW I too would figure that running more then one cron-job back-to-back must be commonplace. Better yet, I’ll create hello1.out, hello2.out, and hello3.out and run them the same night at 1, 2 and 3:00AM and I’ll let you know the result. That should tell us something.

    0 1 * * * /hello1.out
    0 2 * * */hello2.out
    0 3 * * */hello3.out

    Wow jc5000, I feel your pain and you are smarter than a fifth grader.
    Last edited by jc2020; 03-15-2020 at 04:28 AM.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If it is an OS question, why did you not post information about your OS?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by laserlight View Post
    ...midnight only occurs once every day
    I beg to differ, see if you can spot why from the quote
    Anyways what is cron?

    Edit: Oh so it was just a schedular
    Last edited by awsdert; 03-15-2020 at 06:15 AM. Reason: typo

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by jc2020 View Post
    I have 3 well crafted c-files
    Don't be too sure of that, even expert programmers (of which I would like to call myself but not dumb enough to try) can and will make mistakes in their code, just recently I made the mistake of assuming my file offsets were where I wanted them forgetting that a prior read shifted them off by a block, not only giving me incorrect results but corrupting the stored data that would read next

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    When you fill your posts with complaints and heresay, what did you expect.

    Posting evidence of what you actually did is better if you want people to give you actionable things to try. "I tried something and something happened" will just get you ignored.

    Here.
    Code:
    $ cat bar.c
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[]) {
      int value = 0;
      FILE *fp = fopen(argv[1],"r");
      if ( fp ) {
        fscanf(fp,"%d",&value);
        fclose(fp);
      }
      fp = fopen(argv[1],"w");
      fprintf(fp,"%d\n",value+1);
      fclose(fp);
      return 0;
    }
    $ crontab -l
    */5 * * * *	cd /home/user && ./a.out counter1.txt
    */10 * * * *	cd /home/user && ./a.out counter2.txt
    */15 * * * *	cd /home/user && ./a.out counter3.txt
    Which after several hours of waiting results in
    Code:
    Sun 15 Mar 11:45:05 GMT 2020
    Sun 15 Mar 12:45:05 GMT 2020
    ==> counter1.txt <==
    13
    
    ==> counter2.txt <==
    6
    
    ==> counter3.txt <==
    5
    $ date ; head counter*.txt
    Sun 15 Mar 17:23:39 GMT 2020
    ==> counter1.txt <==
    68
    
    ==> counter2.txt <==
    34
    
    ==> counter3.txt <==
    23
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jc2020
    Yep, it’s my terminology and even my English that gets in the way.
    Yes, it is. If you cannot adequately communicate what you're trying to do in English (or some other human natural language), then you will have problems doing the same correctly in code... or in a cronjob, for that matter, since it will not be certain whether the code corresponds to what you intended.

    For example, crontab guru is a useful tool for experimenting and checking cron syntax. But if you don't know how to express what you want to schedule in English, then you will have trouble identifying what to use.

    Quote Originally Posted by awsdert
    I beg to differ, see if you can spot why from the quote
    I'm guessing that you have in mind "day" as in "daytime", but of course that is not what it is meant here, so you can beg, but you're wrong
    Last edited by laserlight; 03-15-2020 at 03:48 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    laserlight, I know what you mean but like you said:
    If you cannot adequately communicate what you're trying to do in English
    But It’s near the same if someone said:
    If you cannot adequately communicate what you're trying to say in English
    You either say day for day and night for night. Midnight is night is the middle of the night.

    Please don’t be angry.
    crontab guru Wow! I can't wait!

    stahta01, good point. I use FreeBSD-10.1 and Debian Jessie Linux. Neither is broke and I'm not going to fix it.

    awsdert, you woke me up. The Windows scheduler. It lead me to the cron-scheduler (strange name):

    Cron Scheduler Service for Complex Cron Jobs - PipeGears so it is possible.


    There are times when one have no choice but to try to explain things the best way he can. I never did this before, check any of my previous threads. My choice was to rip my program apart with pathnames seven or more links deep, or to tell it like it was and hope for the best. I tried to rip but jacked everything up, then I put it back together. I was burn-up burnout.

    It took me 4 days to finally post what I wrote 4 days before fearing that I may have said the wrong thing or too much. Now it’s full of type-o, caps and missing words. There is no excuse, it's just the truth. I just kelp reading, guessing, and testing for days with no success. This is what I do but it did not work for me this time. We all make mistakes.


    Anyway, now I know for a fact it was something I was doing wrong.

    I know this is a professional forum. I will do as much as possilbe to make things cleaner and clearer in the future Thanks Salem

    Seems the same. It took me a while to find and use fscanf. This code itself could have been part of my problem. Thank Goodness I now have the opportunity to find out sooner than latter.


    Code:
    //  over 10 digits I saw problems, so there may be weakness somewhere much deeper.
    //  Adjust to any size below 10 digits seems ok.
    //  The code to big and it need to use buffers.
    //  
    //  weather_id.txt    1000000001
    //  weather_id.c    This file
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int addTo(int x);
    int num = 0;
    int  sum = 0;
    char str[11];
    
    int main() {
    // ####################################################    update ID
    // ####################################################
    FILE *fr, *fw; 
    fr=fopen("/AUTO_ID/weather_id.txt","r");
    fw=fopen("/AUTO_ID/tmp.txt","w");
    
         fscanf(fr, "%d\n", &num);
         sum = addTo(num);
         sprintf( str, "%d", sum );
         fflush(stdout);
    
    fputs(str, fw);
    
    fclose(fr);
    fclose(fw);
     fw=fopen("/AUTO_ID/weather_id.txt","w"); fclose(fw);
    // ......................................................
    // ...................................................... Complete the ID file
    // ......................................................
    fr=fopen("/AUTO_ID/tmp.txt","r");
    fw=fopen("/AUTO_ID/weather_id.txt","w");
    
    char ch;
    int pos;
           fseek(fr, 0L, SEEK_END);
               pos = ftell(fr);
               fseek(fr, 0L, SEEK_SET);
               while (pos--)
              {
                   ch = fgetc(fr);
                    fputc(ch, fw);  }    
    fclose(fr);
    fclose(fw);
    fw=fopen("/AUTO_ID/tmp.txt","w"); fclose(fw);
    
       return 0;
    }
    
    /**  ...........................................  */
    /**  ........    MATH - ADD A NUMBER    ........  */
    /**  ...........................................  */
    
    int addTo(int x) 
    { 
    int m = 1;              /**  add upto any amount  */
        while( x & m )      /**  flip bits to find 0  */
            {  x = x ^ m; 
               m <<= 1; } 
       x = x ^ m;              /**  flip rightmost 0 bit */
        return x; }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jc2020
    You either say day for day and night for night. Midnight is night is the middle of the night.
    Refer to an English dictionary entry on "day":
    a period of 24 hours, especially from twelve o'clock one night to twelve o'clock the next night:
    This is obviously the intended meaning both from context and since we're talking about cron.

    Instead of arguing, it'll be easier just to clarify what you mean: did you mean "run the program at midnight every day" (which is what your cronjob syntax is saying), or did you mean something else involving "every minute"? Notice that in post #2 I did not criticise you for your English: I merely pointed out that your language was unclear and so required clarification.
    Last edited by laserlight; 03-15-2020 at 06:14 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jun 2019
    Posts
    44
    I knew not to challenge the light, but I had to take a crack at it since I know this opportunity may never arise again. It was just a moment of fun. You remember so much and I think you have a photographic mind so we don't doubt you, I know I don't.

    Anyway, Midnight every minute was only a notation with out separators. I used it to see how long I had to wait to see all three c-files result. Then I tested 10 15 etc. minutes. So to run it, go have coffee and come back to see the result in a more reasonable amount of time. Every one minute fills up the log file too fast and I would never use it when all is said and done. It one run daily of three or more c-files to get the mysql csv-file ID update and that's it. That's what I call fun!

    00 0 * * * - - - 0 mean at midnight and 00 mean every minute until 12:59 I believe … and so on.

    0 0 * * * - - - - I notice on the net some sites use it this way. That had me confuse so I’ll be checking tonight and use what may be most recommended.
    Last edited by jc2020; 03-15-2020 at 07:23 PM.

  14. #14
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by jc2020 View Post
    ...
    Code:
    ...
    int main() {
    // ####################################################    update ID
    // ####################################################
    FILE *fr, *fw; 
    fr=fopen("/AUTO_ID/weather_id.txt","r");
    fw=fopen("/AUTO_ID/tmp.txt","w");
    
         fscanf(fr, "%d\n", &num);
         sum = addTo(num);
         sprintf( str, "%d", sum );
         fflush(stdout);
    ...
    Er, why are you flushing stdout when you haven't sent anything to it yet???
    Also instead of opening the files twice under I suggest the following:
    Code:
    weather_file = fopen("/AUTO_ID/weather_id.txt","rw");
    tmp_file = fopen("/AUTO_ID/tmp.txt","rw");
    And closing just once after everything is done, to reset the file offset just use lseek( file, 0, SEEK_SET )
    Finally I believe the corruption is occuring here:
    Code:
    sum = addTo(num);
         sprintf( str, "%d", sum );
         fflush(stdout);
     
    fputs(str, fw); // << here
    The str is only filled with a number earlier and as a result will only override by the digits given, it won't clear existing digits beyond what is given, try appending \n to the earlier %d to ensure a cutoff when fscanf scans the section, also instead of printing to a character array with sprintf just use fprintf and finally why do you not reset the file offset before trying to override the number?
    Last edited by awsdert; 03-15-2020 at 07:21 PM. Reason: Didn't finish my example

  15. #15
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Er just realised I said lseek instead of fseek, would edit the post but I've just turned off my pc to go to bed and editing with a phone is annoyoing to do

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 09-13-2012, 08:57 PM
  2. cron troubles
    By Annonymous in forum Linux Programming
    Replies: 6
    Last Post: 05-09-2012, 03:51 PM
  3. creating cron job
    By nitinmhetre in forum Tech Board
    Replies: 3
    Last Post: 12-29-2006, 10:16 AM
  4. Cron - for all you unix people
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-03-2004, 11:52 PM

Tags for this Thread