Running bash inside cronjobHow to determine why a machine is running slowly?Why does my cronjob not execute my shell-script?Cron job running at 12.00 AM despite changing its timeHow to interpret busybox “top” output?Latexmk, from Makefile, from bash script, from Cron - Latexmk not being executedLog free and topoutput logs in crontab does not workunreasonable memory usageSFTP script not working with cron jobBash script able to run manually, but with cron not working. + Ubuntu 16.04

Professor forcing me to attend a conference, I can't afford even with 50% funding

The preposition for the verb (avenge) - avenge sb/sth (on OR from) sb

How can I portion out frozen cookie dough?

Was it really inappropriate to write a pull request for the company I interviewed with?

Are small insurances worth it?

Is there a logarithm base for which the logarithm becomes an identity function?

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?

When to use a QR code on a business card?

How do we create new idioms and use them in a novel?

Why does Central Limit Theorem break down in my simulation?

Why restrict private health insurance?

I am the person who abides by rules, but breaks the rules. Who am I?

Is there a way to make cleveref distinguish two environments with the same counter?

The (Easy) Road to Code

What is this tube in a jet engine's air intake?

Are E natural minor and B harmonic minor related?

Can one live in the U.S. and not use a credit card?

Are these two graphs isomorphic? Why/Why not?

Do black holes violate the conservation of mass?

Why do phishing e-mails use faked e-mail addresses instead of the real one?

Giving a career talk in my old university, how prominently should I tell students my salary?

When an outsider describes family relationships, which point of view are they using?

Why does this boat have a landing pad? (SpaceX's GO Searcher) Any plans for propulsive capsule landings?

Too soon for a plot twist?



Running bash inside cronjob


How to determine why a machine is running slowly?Why does my cronjob not execute my shell-script?Cron job running at 12.00 AM despite changing its timeHow to interpret busybox “top” output?Latexmk, from Makefile, from bash script, from Cron - Latexmk not being executedLog free and topoutput logs in crontab does not workunreasonable memory usageSFTP script not working with cron jobBash script able to run manually, but with cron not working. + Ubuntu 16.04













1















I have this cronjob to run every minute



*/1 * * * * root sh /test.sh


My /test.sh logs the result of "top" and "free" command. It works fine when I run it manually on terminal with "sh /teste.sh" and saves the output to a nice file, however when the cron job runs it only saves the result of the command "free" below. Check this please:



printf "n" >> "log_lojar_top_free.txt"
printf %s "$(date)" >> "log_lojar_top_free.txt"
printf 't' >> "log_lojar_top_free.txt"
top -b -n 3 -d 1 | grep "Cpu" | tail -n 1 | awk '/^%Cpu(s)/ printf $2' >> "log_lojar_top_free.txt"
printf 't' >> "log_lojar_top_free.txt"
free | awk '/^Mem:/ printf $7' >> "log_lojar_top_free.txt"


What is the error that is causing that only the last line (free) to have it's output loged?










share|improve this question
























  • if you want to run a bash script then use bash, not sh. they're not the same thing. even if sh is a symlink to bash, bash behaves differently if called as sh rather than bash.

    – cas
    Oct 31 '15 at 0:51











  • you can do as cas suggests either by prefixing in crontab with /bin/bash instead of sh, or by starting your script with a shebang line: #!/bin/bash - see if it behaves better once you definitely have it running under bash and not some other shell. Another possibility is that you aren't looking in the right place for the output file. The cron job is running as root and since you didn't specify an output directory, only a file, it will probably save it at / - not a good place to use generally. Oh and BTW you don't need the /1.

    – gogoud
    Oct 31 '15 at 8:13












  • A quick check shows that my implementation of crond writes to the user's home directory. In my case, for root that's /root.

    – roaima
    Nov 3 '15 at 22:51















1















I have this cronjob to run every minute



*/1 * * * * root sh /test.sh


My /test.sh logs the result of "top" and "free" command. It works fine when I run it manually on terminal with "sh /teste.sh" and saves the output to a nice file, however when the cron job runs it only saves the result of the command "free" below. Check this please:



printf "n" >> "log_lojar_top_free.txt"
printf %s "$(date)" >> "log_lojar_top_free.txt"
printf 't' >> "log_lojar_top_free.txt"
top -b -n 3 -d 1 | grep "Cpu" | tail -n 1 | awk '/^%Cpu(s)/ printf $2' >> "log_lojar_top_free.txt"
printf 't' >> "log_lojar_top_free.txt"
free | awk '/^Mem:/ printf $7' >> "log_lojar_top_free.txt"


What is the error that is causing that only the last line (free) to have it's output loged?










share|improve this question
























  • if you want to run a bash script then use bash, not sh. they're not the same thing. even if sh is a symlink to bash, bash behaves differently if called as sh rather than bash.

    – cas
    Oct 31 '15 at 0:51











  • you can do as cas suggests either by prefixing in crontab with /bin/bash instead of sh, or by starting your script with a shebang line: #!/bin/bash - see if it behaves better once you definitely have it running under bash and not some other shell. Another possibility is that you aren't looking in the right place for the output file. The cron job is running as root and since you didn't specify an output directory, only a file, it will probably save it at / - not a good place to use generally. Oh and BTW you don't need the /1.

    – gogoud
    Oct 31 '15 at 8:13












  • A quick check shows that my implementation of crond writes to the user's home directory. In my case, for root that's /root.

    – roaima
    Nov 3 '15 at 22:51













1












1








1








I have this cronjob to run every minute



*/1 * * * * root sh /test.sh


My /test.sh logs the result of "top" and "free" command. It works fine when I run it manually on terminal with "sh /teste.sh" and saves the output to a nice file, however when the cron job runs it only saves the result of the command "free" below. Check this please:



printf "n" >> "log_lojar_top_free.txt"
printf %s "$(date)" >> "log_lojar_top_free.txt"
printf 't' >> "log_lojar_top_free.txt"
top -b -n 3 -d 1 | grep "Cpu" | tail -n 1 | awk '/^%Cpu(s)/ printf $2' >> "log_lojar_top_free.txt"
printf 't' >> "log_lojar_top_free.txt"
free | awk '/^Mem:/ printf $7' >> "log_lojar_top_free.txt"


What is the error that is causing that only the last line (free) to have it's output loged?










share|improve this question
















I have this cronjob to run every minute



*/1 * * * * root sh /test.sh


My /test.sh logs the result of "top" and "free" command. It works fine when I run it manually on terminal with "sh /teste.sh" and saves the output to a nice file, however when the cron job runs it only saves the result of the command "free" below. Check this please:



printf "n" >> "log_lojar_top_free.txt"
printf %s "$(date)" >> "log_lojar_top_free.txt"
printf 't' >> "log_lojar_top_free.txt"
top -b -n 3 -d 1 | grep "Cpu" | tail -n 1 | awk '/^%Cpu(s)/ printf $2' >> "log_lojar_top_free.txt"
printf 't' >> "log_lojar_top_free.txt"
free | awk '/^Mem:/ printf $7' >> "log_lojar_top_free.txt"


What is the error that is causing that only the last line (free) to have it's output loged?







linux ubuntu centos






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 min ago









Guillermo Mosse

1031




1031










asked Oct 30 '15 at 22:46









SamulSamul

18117




18117












  • if you want to run a bash script then use bash, not sh. they're not the same thing. even if sh is a symlink to bash, bash behaves differently if called as sh rather than bash.

    – cas
    Oct 31 '15 at 0:51











  • you can do as cas suggests either by prefixing in crontab with /bin/bash instead of sh, or by starting your script with a shebang line: #!/bin/bash - see if it behaves better once you definitely have it running under bash and not some other shell. Another possibility is that you aren't looking in the right place for the output file. The cron job is running as root and since you didn't specify an output directory, only a file, it will probably save it at / - not a good place to use generally. Oh and BTW you don't need the /1.

    – gogoud
    Oct 31 '15 at 8:13












  • A quick check shows that my implementation of crond writes to the user's home directory. In my case, for root that's /root.

    – roaima
    Nov 3 '15 at 22:51

















  • if you want to run a bash script then use bash, not sh. they're not the same thing. even if sh is a symlink to bash, bash behaves differently if called as sh rather than bash.

    – cas
    Oct 31 '15 at 0:51











  • you can do as cas suggests either by prefixing in crontab with /bin/bash instead of sh, or by starting your script with a shebang line: #!/bin/bash - see if it behaves better once you definitely have it running under bash and not some other shell. Another possibility is that you aren't looking in the right place for the output file. The cron job is running as root and since you didn't specify an output directory, only a file, it will probably save it at / - not a good place to use generally. Oh and BTW you don't need the /1.

    – gogoud
    Oct 31 '15 at 8:13












  • A quick check shows that my implementation of crond writes to the user's home directory. In my case, for root that's /root.

    – roaima
    Nov 3 '15 at 22:51
















if you want to run a bash script then use bash, not sh. they're not the same thing. even if sh is a symlink to bash, bash behaves differently if called as sh rather than bash.

– cas
Oct 31 '15 at 0:51





if you want to run a bash script then use bash, not sh. they're not the same thing. even if sh is a symlink to bash, bash behaves differently if called as sh rather than bash.

– cas
Oct 31 '15 at 0:51













you can do as cas suggests either by prefixing in crontab with /bin/bash instead of sh, or by starting your script with a shebang line: #!/bin/bash - see if it behaves better once you definitely have it running under bash and not some other shell. Another possibility is that you aren't looking in the right place for the output file. The cron job is running as root and since you didn't specify an output directory, only a file, it will probably save it at / - not a good place to use generally. Oh and BTW you don't need the /1.

– gogoud
Oct 31 '15 at 8:13






you can do as cas suggests either by prefixing in crontab with /bin/bash instead of sh, or by starting your script with a shebang line: #!/bin/bash - see if it behaves better once you definitely have it running under bash and not some other shell. Another possibility is that you aren't looking in the right place for the output file. The cron job is running as root and since you didn't specify an output directory, only a file, it will probably save it at / - not a good place to use generally. Oh and BTW you don't need the /1.

– gogoud
Oct 31 '15 at 8:13














A quick check shows that my implementation of crond writes to the user's home directory. In my case, for root that's /root.

– roaima
Nov 3 '15 at 22:51





A quick check shows that my implementation of crond writes to the user's home directory. In my case, for root that's /root.

– roaima
Nov 3 '15 at 22:51










2 Answers
2






active

oldest

votes


















1














As suggested by others, try to direct use the bash shebang in your script or prefix by using bash instead of sh. For I don't know what system you're actually running, I recently ran into trouble calling a script usign /bin/sh -c myscript.sh under ubuntu which is a debian derivate which uses dash instead of bash.



Maybe this is the key to your problem.



EDIT: I've got it working with this crontab entry, done as root with crontab -e:



*/1 * * * * /bin/bash -c "/test.sh"





share|improve this answer

























  • Good catch re dash. However, the OP's script appears to run equivalently under bash, sh (for some value of sh) and dash.

    – roaima
    Nov 3 '15 at 23:22


















0














This version works for me. However, as your own script also works here I'm not entirely sure this will fix your problem.



System-wide crontab entry (omit the root field if this is root's own crontab, as accessed with crontab -e):



* * * * * root /root/test.sh


As far as I can detemine, your script takes the third iteration from top and collects the CPU usermode percentage. It also collects the cached memory value from free. Here I've dispensed with free and extracted the same value from the third iteration of top. Copy this script to /root/test.sh (and make it executable):



#!/bin/bash
top -b -n 3 -d 1 |
awk -v date="$(date)" '
/^%Cpu/ cpu=$2
/cached Mem/ cached=$9
END printf "n%st%st%s", date, cpu, cached
' >> /root/log_lojar_top_free.txt


Make the script executable:



chmod +x /root/test.sh





share|improve this answer






















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f239863%2frunning-bash-inside-cronjob%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    As suggested by others, try to direct use the bash shebang in your script or prefix by using bash instead of sh. For I don't know what system you're actually running, I recently ran into trouble calling a script usign /bin/sh -c myscript.sh under ubuntu which is a debian derivate which uses dash instead of bash.



    Maybe this is the key to your problem.



    EDIT: I've got it working with this crontab entry, done as root with crontab -e:



    */1 * * * * /bin/bash -c "/test.sh"





    share|improve this answer

























    • Good catch re dash. However, the OP's script appears to run equivalently under bash, sh (for some value of sh) and dash.

      – roaima
      Nov 3 '15 at 23:22















    1














    As suggested by others, try to direct use the bash shebang in your script or prefix by using bash instead of sh. For I don't know what system you're actually running, I recently ran into trouble calling a script usign /bin/sh -c myscript.sh under ubuntu which is a debian derivate which uses dash instead of bash.



    Maybe this is the key to your problem.



    EDIT: I've got it working with this crontab entry, done as root with crontab -e:



    */1 * * * * /bin/bash -c "/test.sh"





    share|improve this answer

























    • Good catch re dash. However, the OP's script appears to run equivalently under bash, sh (for some value of sh) and dash.

      – roaima
      Nov 3 '15 at 23:22













    1












    1








    1







    As suggested by others, try to direct use the bash shebang in your script or prefix by using bash instead of sh. For I don't know what system you're actually running, I recently ran into trouble calling a script usign /bin/sh -c myscript.sh under ubuntu which is a debian derivate which uses dash instead of bash.



    Maybe this is the key to your problem.



    EDIT: I've got it working with this crontab entry, done as root with crontab -e:



    */1 * * * * /bin/bash -c "/test.sh"





    share|improve this answer















    As suggested by others, try to direct use the bash shebang in your script or prefix by using bash instead of sh. For I don't know what system you're actually running, I recently ran into trouble calling a script usign /bin/sh -c myscript.sh under ubuntu which is a debian derivate which uses dash instead of bash.



    Maybe this is the key to your problem.



    EDIT: I've got it working with this crontab entry, done as root with crontab -e:



    */1 * * * * /bin/bash -c "/test.sh"






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 3 '15 at 23:41

























    answered Nov 3 '15 at 23:19









    ferdyferdy

    1496




    1496












    • Good catch re dash. However, the OP's script appears to run equivalently under bash, sh (for some value of sh) and dash.

      – roaima
      Nov 3 '15 at 23:22

















    • Good catch re dash. However, the OP's script appears to run equivalently under bash, sh (for some value of sh) and dash.

      – roaima
      Nov 3 '15 at 23:22
















    Good catch re dash. However, the OP's script appears to run equivalently under bash, sh (for some value of sh) and dash.

    – roaima
    Nov 3 '15 at 23:22





    Good catch re dash. However, the OP's script appears to run equivalently under bash, sh (for some value of sh) and dash.

    – roaima
    Nov 3 '15 at 23:22













    0














    This version works for me. However, as your own script also works here I'm not entirely sure this will fix your problem.



    System-wide crontab entry (omit the root field if this is root's own crontab, as accessed with crontab -e):



    * * * * * root /root/test.sh


    As far as I can detemine, your script takes the third iteration from top and collects the CPU usermode percentage. It also collects the cached memory value from free. Here I've dispensed with free and extracted the same value from the third iteration of top. Copy this script to /root/test.sh (and make it executable):



    #!/bin/bash
    top -b -n 3 -d 1 |
    awk -v date="$(date)" '
    /^%Cpu/ cpu=$2
    /cached Mem/ cached=$9
    END printf "n%st%st%s", date, cpu, cached
    ' >> /root/log_lojar_top_free.txt


    Make the script executable:



    chmod +x /root/test.sh





    share|improve this answer



























      0














      This version works for me. However, as your own script also works here I'm not entirely sure this will fix your problem.



      System-wide crontab entry (omit the root field if this is root's own crontab, as accessed with crontab -e):



      * * * * * root /root/test.sh


      As far as I can detemine, your script takes the third iteration from top and collects the CPU usermode percentage. It also collects the cached memory value from free. Here I've dispensed with free and extracted the same value from the third iteration of top. Copy this script to /root/test.sh (and make it executable):



      #!/bin/bash
      top -b -n 3 -d 1 |
      awk -v date="$(date)" '
      /^%Cpu/ cpu=$2
      /cached Mem/ cached=$9
      END printf "n%st%st%s", date, cpu, cached
      ' >> /root/log_lojar_top_free.txt


      Make the script executable:



      chmod +x /root/test.sh





      share|improve this answer

























        0












        0








        0







        This version works for me. However, as your own script also works here I'm not entirely sure this will fix your problem.



        System-wide crontab entry (omit the root field if this is root's own crontab, as accessed with crontab -e):



        * * * * * root /root/test.sh


        As far as I can detemine, your script takes the third iteration from top and collects the CPU usermode percentage. It also collects the cached memory value from free. Here I've dispensed with free and extracted the same value from the third iteration of top. Copy this script to /root/test.sh (and make it executable):



        #!/bin/bash
        top -b -n 3 -d 1 |
        awk -v date="$(date)" '
        /^%Cpu/ cpu=$2
        /cached Mem/ cached=$9
        END printf "n%st%st%s", date, cpu, cached
        ' >> /root/log_lojar_top_free.txt


        Make the script executable:



        chmod +x /root/test.sh





        share|improve this answer













        This version works for me. However, as your own script also works here I'm not entirely sure this will fix your problem.



        System-wide crontab entry (omit the root field if this is root's own crontab, as accessed with crontab -e):



        * * * * * root /root/test.sh


        As far as I can detemine, your script takes the third iteration from top and collects the CPU usermode percentage. It also collects the cached memory value from free. Here I've dispensed with free and extracted the same value from the third iteration of top. Copy this script to /root/test.sh (and make it executable):



        #!/bin/bash
        top -b -n 3 -d 1 |
        awk -v date="$(date)" '
        /^%Cpu/ cpu=$2
        /cached Mem/ cached=$9
        END printf "n%st%st%s", date, cpu, cached
        ' >> /root/log_lojar_top_free.txt


        Make the script executable:



        chmod +x /root/test.sh






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 3 '15 at 23:11









        roaimaroaima

        45.4k757123




        45.4k757123



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f239863%2frunning-bash-inside-cronjob%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mobil Contents History Mobil brands Former Mobil brands Lukoil transaction Mobil UK Mobil Australia Mobil New Zealand Mobil Greece Mobil in Japan Mobil in Canada Mobil Egypt See also References External links Navigation menuwww.mobil.com"Mobil Corporation"the original"Our Houston campus""Business & Finance: Socony-Vacuum Corp.""Popular Mechanics""Lubrite Technologies""Exxon Mobil campus 'clearly happening'""Toledo Blade - Google News Archive Search""The Lion and the Moose - How 2 Executives Pulled off the Biggest Merger Ever""ExxonMobil Press Release""Lubricants""Archived copy"the original"Mobil 1™ and Mobil Super™ motor oil and synthetic motor oil - Mobil™ Motor Oils""Mobil Delvac""Mobil Industrial website""The State of Competition in Gasoline Marketing: The Effects of Refiner Operations at Retail""Mobil Travel Guide to become Forbes Travel Guide""Hotel Rankings: Forbes Merges with Mobil"the original"Jamieson oil industry history""Mobil news""Caltex pumps for control""Watchdog blocks Caltex bid""Exxon Mobil sells service station network""Mobil Oil New Zealand Limited is New Zealand's oldest oil company, with predecessor companies having first established a presence in the country in 1896""ExxonMobil subsidiaries have a business history in New Zealand stretching back more than 120 years. We are involved in petroleum refining and distribution and the marketing of fuels, lubricants and chemical products""Archived copy"the original"Exxon Mobil to Sell Its Japanese Arm for $3.9 Billion""Gas station merger will end Esso and Mobil's long run in Japan""Esso moves to affiliate itself with PC Optimum, no longer Aeroplan, in loyalty point switch""Mobil brand of gas stations to launch in Canada after deal for 213 Loblaws-owned locations""Mobil Nears Completion of Rebranding 200 Loblaw Gas Stations""Learn about ExxonMobil's operations in Egypt""Petrol and Diesel Service Stations in Egypt - Mobil"Official websiteExxon Mobil corporate websiteMobil Industrial official websiteeeeeeeeDA04275022275790-40000 0001 0860 5061n82045453134887257134887257

            Frič See also Navigation menuinternal link

            Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant