0

I have followed up this question successfully, Using CRON jobs to visit url?, to maintain the following Cron task:

*/30 * * * * wget -O - https://example.com/operation/lazy-actions?lt=SOME_ACCESS_TOKEN_HERE >/dev/null 2>&1

The above Cron task works fine and it visits the URL periodically every 30 minutes.

However, the access token is recorded in a text file found in /home/myaccount/www/site/aToken.txt, the aToken file is very simple text file of one line which just contains the token string.

I have tried to read its contents and pass, using cat, it to the crontab command like the following:

*/30 * * * * wget -O - https://example.com/operation/lazy-actions?lt=|cat /home/myaccount/www/site/aToken.txt| >/dev/null 2>&1

However, the above solution has been failed to run the cronjob.

I edit Cronjobs using crontab -e using nano on Ubuntu 16.04

SaidbakR
  • 13,303
  • 20
  • 101
  • 195
  • Since you have the access token stored in a physical file in your userfile .. Can't you just create a bash, or shell script that simply does everything you want it to do? IE open the token file, save to variable, wget website, write success / error logs etc etc etc ? – Zak Jun 19 '18 at 17:29
  • @Zak I'm not expert in bash and shell, and I had tried to keep things simple and clear. – SaidbakR Jun 19 '18 at 17:33

2 Answers2

1

This is a quick solution that will do exactly what you want without the complicated one-liner:

Create this file in your myaccount -- You may also put it into your bin directory if you so desire just remember where you put it so you can call it from your CRON. Also make sure the user has permissions to read/write to the directory the sh file is in

wget.sh

#!/bin/bash

#simple cd -- change directory
cd /home/myaccount/www/site/  

#grab token into variable aToken
aToken=`cat aToken.txt`  

#simple cd -- move to wget directory
cd /wherever/you/want/the/wget/results/saved 

#Notice the $ -- This is how we let the shell know that aToken is a variable = $aToken
#wget -O - https://example.com/operation/lazy-actions?lt=$aToken
wget -q -nv -O /tmp/wget.txt https://example.com/operation/lazy-actions?lt=$aToken >/dev/null 2>/dev/null

# You can writle logs etc etc afterward here.  IE
echo "Job was successful" >> /dir/to/logs/success.log

Then simply call this file with your CRON like you are doing already.

*/30 * * * * sh /home/myaccount/www/site/wget.sh >/dev/null 2>&1
Zak
  • 6,976
  • 2
  • 26
  • 48
  • It looks like nice solution. However, I would not like any output from `wget` as shown in the cron job. – SaidbakR Jun 19 '18 at 19:03
  • 1
    I've edited the wget inside to store the info in a temp file, then the dev/null should remove the temp file .. -- You should be left with a clean wget after that .. – Zak Jun 19 '18 at 19:17
  • and if that doesn't work -- `--quiet` instead of `-q` may work depending on your setup .. – Zak Jun 19 '18 at 19:22
  • Thank you @Zak very much for your help, but I have found the solution that I, exactly, need. – SaidbakR Jun 19 '18 at 19:26
1

Built on that question, Concatenate in bash the output of two commands without newline character, I have got the following simple solution:

wget -O - https://example.com/operation/lazy-actions?lt="$(cat /home/myaccount/www/site/aToken.txt)" >/dev/null 2>&1

Where it able to read the contents of the text file and then echoed to the command stream.

SaidbakR
  • 13,303
  • 20
  • 101
  • 195